Chào các bạn hôm nay chúng ta sẽ cùng học cách tải tập tin lên bằng phương thức lập trình hướng đối tượng trong lập trình PHP cơ bản. Trước tiên Tạo 1 PHP Project tên là OOP, xong tạo 1 Folder tên là upload, trong Foder upload tạo tiếp 1 Foder tên class, trong class tạo 1 foder images, và 3 tập tin php, lần lượt tên, upload.php , upload.class.php , process.php
- File upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http–equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Upload Form</title>
</head>
<body>
<h1>Upload hình ảnh</h1>
<form id=“myform” name=“form” method=“post” enctype=“multipart/form-data” action=“process.php”>
<input type=“file” name=“upload” id=“upload”></input>
<br/>
<input type=“submit” name=“button” id=“button” value=“Submit”></input>
</form>
</body>
</html>
|
- File proces.php xử lý file upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
require_once ‘upload.class.php’;
if(!empty($_POST)){
$upload =new Upload(‘upload’);//khoi tao doi tuong, ham up load yeu cau filename, chinh la ten input “upload”
$upload–>setFileExtension(‘gif|jpg|png’);//3 duoi gif|jpg|png
$upload–>setFileSize(100);//kiem tra size ko vuot hon 100kb
$upload–>setUploadDir(‘images/’);
//kiem tra truoc khi upload
if($upload–>isVail()==false){
$upload–>upload(true);
}else{
echo “<pre>”;
print_r($upload–>_errors);
echo “</pre>”;
}
}
|
- File upload.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
<?php
class Upload{
//bien luu tru ten tap tin upload
var $_fileName;
//bien luu tru kich thuoc cua tap tin upload
var $_fileSize;
//Bien luu tru phan mo rong cua tap tin upload
var $_fileExtentsion;
//bien luu tru duong dan cua thu muc tap tin upload
var $_fileTmp;
//bien luu truong duong dan tren server cua tap tin upload
var $_uploadDir;
//bien luu tru error
var $_errors;
//Phuong thuc khoi tao doi tuong
function __construct($file_name){
$fileInfon = $_FILES[$file_name];
$this–>_fileName = $fileInfon[‘name’];
$this–>_fileSize = $fileInfon[‘size’];
$this–>_fileExtentsion = $this–>getFileExtension();
$this–>_fileTmp = $fileInfon[‘tmp_name’];
}
//phuong thuc lay thanh phan mo rong cua tap tin upload
function getFileExtension(){
$subject = $this–>_fileName;
$pattern = ‘#.([^.]+)$#i’;
preg_match($pattern, $subject,$matches);
return $matches[1];
}
//phuong thuc thiet lap thanh phan mo rong duoc phep upload
//param string (ex; gif|ipg|png) day la tham so ta se truyen vao
function setFileExtension($value){
$subject = $this–>_fileExtentsion; //se co gia tri la Jpg
$pattern = ‘#(‘. $value . ‘)#i’;
if(preg_match($pattern, $subject)!==1){
$this–>_errors[] = ‘phan mo rong ko phu hop’;
}
}
//phuong thuc thiet lap kich thuoc toi da duoc phep upload
//param in (ex: 100=100kb)
function setFileSize($value){
$size = $value * 1024;
if($this–>_fileSize > $size){
$this–>_errors[] = ‘Kich thuoc nay khong phu hop voi yeu cau’;
}
}
//phuong thuc thiet lap thu muc chua tap tin tren sever
//param string (ex :images/)
function setUploadDir($value){
if(file_exists($value)){
$this–>_uploadDir = $value;//neu no ton tai thi gan no vao thuoc tinh Dir
}else{
$this–>_errors[] = ‘thu muc khong he ton tai’;
}
}
//Phuong thuc kiem tra setFileExtension hop voi setFileSize hay ko
//Phuong thuc isVail de kiem tra tap tin cho truoc co phu hop hay ko
function isVail(){
$flagErr = false;
if(count($this–>_errors)>0){
$flagErr = true;
}
return $flagErr;
}
//phuong thuc upload tap tin
//trong phuong thuc Upload ta su dung ham copy
function upload($rename = false, $prefix = ‘file_’){
if($rename == false){
$source = $this–>_fileTmp;
$dest = $this–>_uploadDir .$this–>_fileName;
}else {
$source = $this–>_fileTmp;
$dest = $this–>_uploadDir . $prefix .time() . ‘.’ . $this–>_fileExtentsion;
}
copy($source, $dest);
}
}
|
Chúc các bạn thành công.