PHP multi-file Upload comprehension summary _ PHP Tutorial

Source: Internet
Author: User
PHP multi-file Upload comprehension summary. Add [] to the name of the input tag FILE type of the html file to create an array in the html file. for example, the name is pictures, the multi-FILE reference name is pictures []. the following example uploads the name of the FILE in the HTML input tag FILE type and adds []. the function is to create an array for PHP in HTML, for example, the name is pictures, and the multi-file reference name is pictures []. The example is as follows:
The code is as follows:

// Example in the manual.

Select a file and click Upload

The code is as follows:

Print_r ($ _ FILES );
?>

View source files:

The code is as follows:
Array
(
[Pictures] => Array
(
[Name] => Array
(
[0] => file1.txt
[1] => file2.txt
[2] => file3.txt
)
[Type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
[2] => application/octet-stream
)
[Tmp_name] => Array
(
[0] => D: EasyPHPtmpphp47.tmp
[1] => D: EasyPHPtmpphp48.tmp
[2] => D: EasyPHPtmpphp49.tmp
)
[Error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[Size] => Array
(
[0] = & gt; 94289
[1] => 65536.
[2] => 102400.
)
)
)

Assume the name is/file1.txt ?? And/file2.txt FILES are submitted, then the value of $ _ FILES ['pictures'] ['name'] [0] will be file1.txt, the value of $ _ FILES ['pictures'] ['name'] [1] is file2.txt. When _ files0000'file2.txt '] ['size'] [0] will contain the size of the file file1.txt,

With the above information, it is easy to implement multifile Upload.

The code is as follows:

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 ('The File for upload is not found! ');
}
}

$ 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 ('upload not allowed '. $ up_file ['type'].' format file! ');

If ($ up_file ['size']/1024> $ this-> up_max ){
Exit ('cannot upload 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 ('upload is not allowed. '. $ up_ext.' format file! ');

If ($ up_file ['size']/1024> $ this-> up_max ){
Exit ('cannot upload files larger than'. $ this-> up_max. 'k! ');
}
}
}

$ This-> uploading ();
}

Private function uploading (){
If (io: dircreate ($ 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 enabled! ');
}
} Else {
Exit ('upload directory creation failed! ');
}
}

Public function getupload (){
Return empty ($ this-> up_ret )? False: $ this-> up_ret;
}

Function _ destruct (){}
}
?>

We can see a for ($ I = 0; $ I <$ up_cnt; $ I ++), which is used to traverse the instances mentioned above.


...

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.