The processing method of PHP multi-file uploading

Source: Internet
Author: User
Tags explode file upload php tutorial

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-&gt;up_name = $name;


!empty ($ext) &amp;&amp; $this-&gt;up_ext = $ext;


$this-&gt;up_rename = $rename;


$this-&gt;up_dir = Website_dirroot.


$globals [' Cfg_upload_path '];


$this-&gt;initupload ();


} else {


Exit (' Upload file domain name is empty, initialization failed! ');


}


}

Private Function Initupload () {


if (Is_array ($_files[$this-&gt;up_name])) {


$up _arr = count ($_files[$this-&gt;up_name]);


$up _all = count ($_files[$this-&gt;up_name], 1);


$up _cnt = ($up _all-$up _arr)/$up _arr;


for ($i = 0; $i &lt; $up _cnt; $i + +) {


if ($_files[$this-&gt;up_name][' error '] [$i]!= 4) {


$this-&gt;up_files[] = Array (


' Tmp_name ' =&gt; $_files[$this-&gt;up_name][' tmp_name ' [$i],


' Name ' =&gt; $_files[$this-&gt;up_name][' name ' [$i],


' Type ' =&gt; $_files[$this-&gt;up_name][' type ' [$i],


' Size ' =&gt; $_files[$this-&gt;up_name][' size ' [$i],


' Error ' =&gt; $_files[$this-&gt;up_name][' ERROR ' [$i]


);


}


}


$this-&gt;up_num = count ($this-&gt;up_files);


} else {


if (Isset ($_files[$this-&gt;up_name])) {


$this-&gt;up_files = Array (


' Tmp_name ' =&gt; $_files[$this-&gt;up_name][' tmp_name '],


' Name ' =&gt; $_files[$this-&gt;up_name][' name '],


' Type ' =&gt; $_files[$this-&gt;up_name][' type '],


' Size ' =&gt; $_files[$this-&gt;up_name][' size '],


' Error ' =&gt; $_files[$this-&gt;up_name][' ERROR ']


);


$this-&gt;up_num = 1;


} else {


Exit (' not find the file that needs upload! ');


}


}





$this-&gt;chkupload ();


}

Private Function Chkupload () {


if (Empty ($this-&gt;up_ext)) {


$up _mime = Array (' image/wbmp ', ' image/bmp ', ' image/gif ', ' image/pjpeg ', ' image/x-png ');


foreach ($this-&gt;up_files as $up _file) {


$up _ALLW = false;


foreach ($up _mime as $mime) {


if ($up _file[' type '] = = $mime) {


$up _ALLW = true; Break


}


}


! $up _allw &amp;&amp; exit (' not allowed to upload '. $up _file[' type ']. ' File in a format! ');

if ($up _file[' size ']/1024 &gt; $this-&gt;up_max) {


Exit (' Do not allow uploading of files larger than '. $this-&gt;up_max. ' K! ');


}


}


} else {


foreach ($this-&gt;up_files as $up _file) {


$up _ext = end (Explode ('. ', $up _file[' name '));

$up _ALLW = false;


foreach ($this-&gt;up_ext as $ext) {


if ($up _ext = = $ext) {


$up _ALLW = true; Break


}


}


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $up _allw &amp;&amp; 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-&gt;up_dir)) {


if (chmod ($this-&gt;up_dir, 0777)) {


if (!empty ($this-&gt;up_files)) {


foreach ($this-&gt;up_files as $up _file) {


if (Is_uploaded_file ($up _file[' Tmp_name ')) {


$file _name = $up _file[' name '];


if ($this-&gt;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-&gt;up_dir. '/'. $file _name;

if (Move_uploaded_file ($up _file[' tmp_name '], $file _name)) {


$this-&gt;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.