PHP Tutorial Multi-File Upload processing method
About multiple file Upload We talked a lot, the following is a simple common form multiple file upload class, need friends can download a trial.
<?php
Class Upload {
Public $up _ext=array (), $up _max=5210, $up _dir;
Private $up _name, $up _rename=true, $up _num=0, $up _files=array (), $up _ret=array ();
function __construct ($name, $ext =array (), $rename =true) {
if (!empty ($name)) {
$this->up_name = $name;
!empty ($ext) && $this->up_ext = $ext;
$this->up_rename = $rename;
$this->up_dir = Website_dirroot.
$globals [' Cfg_upload_path '];
$this->initupload ();
} else {
Exit (' Upload file domain name is empty, initialization failed! ');
}
}
Private Function Initupload () {
if (Is_array ($_files[$this->up_name])) {
$up _arr = count ($_files[$this->up_name]);
$up _all = count ($_files[$this->up_name], 1);
$up _cnt = ($up _all-$up _arr)/$up _arr;
for ($i = 0; $i < $up _cnt; $i + +) {
if ($_files[$this->up_name][' error '] [$i]!= 4) {
$this->up_files[] = Array (
' Tmp_name ' => $_files[$this->up_name][' tmp_name ' [$i],
' Name ' => $_files[$this->up_name][' name ' [$i],
' Type ' => $_files[$this->up_name][' type ' [$i],
' Size ' => $_files[$this->up_name][' size ' [$i],
' Error ' => $_files[$this->up_name][' ERROR ' [$i]
);
}
}
$this->up_num = count ($this->up_files);
} else {
if (Isset ($_files[$this->up_name])) {
$this->up_files = Array (
' Tmp_name ' => $_files[$this->up_name][' tmp_name '],
' Name ' => $_files[$this->up_name][' name '],
' Type ' => $_files[$this->up_name][' type '],
' Size ' => $_files[$this->up_name][' size '],
' Error ' => $_files[$this->up_name][' ERROR ']
);
$this->up_num = 1;
} else {
Exit (' not find the file that needs upload! ');
}
}
$this->chkupload ();
}
Private Function Chkupload () {
if (Empty ($this->up_ext)) {
$up _mime = Array (' image/wbmp ', ' image/bmp ', ' image/gif ', ' image/pjpeg ', ' image/x-png ');
foreach ($this->up_files as $up _file) {
$up _ALLW = false;
foreach ($up _mime as $mime) {
if ($up _file[' type '] = = $mime) {
$up _ALLW = true; Break
}
}
! $up _allw && exit (' not allowed to upload '. $up _file[' type ']. ' File in a format! ');
if ($up _file[' size ']/1024 > $this->up_max) {
Exit (' Do not allow uploading of files larger than '. $this->up_max. ' K! ');
}
}
} else {
foreach ($this->up_files as $up _file) {
$up _ext = end (Explode ('. ', $up _file[' name '));
$up _ALLW = false;
foreach ($this->up_ext as $ext) {
if ($up _ext = = $ext) {
$up _ALLW = true; Break
}
}
$up _allw && exit (' not allowed to upload. ') $up _ext. ' Format Files! ');
if ($up _file[' size ']/1024 > $this->up_max) {
exit (' Do not allow uploading of files larger than '. $this->up_max. ') ');
}
}
}
$this->uploading ();
}
Private function uploading () {
if (io::d ircreate ($this->up_dir)) {
if (chmod ($this->up_dir, 0777)) {
if (!empty ($this->up_files)) {
foreach ($this->up_files as $up _file) {
if (Is_uploaded_file ($up _file[' Tmp_name ')) {
$file _name = $up _file[' name '];
if ($this->up_rename) {
$file _ext = end (Explode ('. ', $file _name));
$file _rnd = substr (MD5 (UNIQID ()), Mt_rand (0, 26), 6);
$file _name = Date (' Ymdhis '). ' _ '. $file _rnd. '. $file _ext;
}
$file _name = $this->up_dir. '/'. $file _name;
if (Move_uploaded_file ($up _file[' tmp_name '], $file _name)) {
$this->up_ret[] = str_replace (Website_dirroot, ', $file _name);
} else {
Exit (' File upload failed! ');
}
}
}
}
} else {
Exit (' Write permission not turned on! ');
}
} else {
Exit (' Upload directory creation failed! ');
}
}
Public Function Getupload () {
return empty ($this->up_ret)? False: $this->up_ret;
}
function __destruct () {}
}
?>
In the case of a large number of files or large volume files, you can consider invoking the component resolution (as mentioned in the SWFUpload component). However, some cases only need to pass a few files, and the file volume is not too large, in this case, the use of components is a bit of a nut to kill chicken feeling, through the HTML <input type= "file" > form can realize the required functions, the key is the processing of background receiving procedures.
PHP processing upload is very convenient, upload file information through the server automatically processed into the $_files array, developers only need to use the built-in processing function simple operation can be. The ASP tutorial developer is not so lucky, the official does not provide the direct processing method, needs the developer to design, then needs the developer to understand IIS to Enctype= "Multipart/form-data" the form processing way, the IIS takes Enctype= " Multipart/form-data "form submitted data stored as binary data, returned to the developer in binary format, developers need to use LenB, MidB byte processing function to analyze the uploaded content, the client sent the specific form data format, you can understand the next HTTP Knowledge of rfc1867 protocol transfer format.