PHP file upload as many files, _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Php files are uploaded as many files ,. Php files are uploaded as many files. before talking nonsense, I declare that this article is based on understanding php single file upload, so I will not repeat the file upload server configuration here, table php files uploaded as many files,

Before talking nonsense, we should first declare that this article is based on the understanding of php single-file upload, so we will not go into details about the file upload server configuration here, and pay attention to the form settings.

There are not many theme statements. There are two writing methods in the request page (only the form part is displayed. the preceding three files are uploaded as an example .)

    

 
  
Select my uploaded files
          
          
          
  
 
 
  
Select my uploaded files
          
          
          
      
 

The two comparisons show that only the name is different. The first one sets the name as an array, and the second one is a method that we usually set easily.

Although on the surface, the $ _ FILES actually submitted to the doAction. php page are quite different.

The first $ _ FILES is a three-dimensional array, and the second is a two-dimensional array, as follows:

Function getFiles () {foreach ($ _ FILES as $ file) {$ fileNum = count ($ file ['name']); if ($ fileNum = 1) {$ files = $ file;} else {for ($ I = 0; $ I <$ fileNum; $ I ++) {$ files [$ I] ['name'] = $ file ['name'] [$ I]; $ files [$ I] ['type'] = $ file ['type'] [$ I]; $ files [$ I] ['tmp _ name'] = $ file ['tmp _ name'] [$ I]; $ files [$ I] ['error'] = $ file ['error'] [$ I]; $ files [$ I] ['size'] = $ file ['size'] [$ I] ;}} return $ files ;}

Use this function to convert $ _ FILES to the following format:

  

At this moment, the two Upload methods are at the same starting point. The following work is to compile the uploadFile () function to upload each file, which is also the focus of this article.

File upload function:

Function uploadFile ($ file, $ path = '. /uploads ', $ max_size, $ allowExt) {$ filename = $ file ['name']; $ type = $ file ['type']; $ temp_name = $ file ['tmp _ name']; $ error = $ file ['error']; $ size = $ file ['size']; if ($ error = UPLOAD_ERR_ OK) {if ($ size> $ max_size) {$ res ['Mes '] = $ filename. "The file size exceeds the specified Upload size";} $ ext = getExt ($ filename); if (! In_array ($ ext, $ allowExt) {$ res ['Mes '] = $ filename.' the file name does not conform to the specifications ';} if (! Is_uploaded_file ($ temp_name) {$ res ['Mes '] = $ filename. "The file is not uploaded through the http post method";} if (@ $ res) {return $ res;} if (! File_exists ($ path) {mkdir ($ path, 0777, true); chmod ($ path, 0777) ;}$ fname = getUniName (); $ destination = $ path. '/'. $ fname. '. '. $ ext; if (move_uploaded_file ($ temp_name, $ destination) {$ res ['Mes '] = $ filename. 'uploaded successfully'; $ res ['dest'] = $ destination;} else {$ res ['Mes '] = $ filename. "File upload failed" ;}}else {switch ($ error) {case '1': $ res ['Mes '] = "exceeds the size of the configuration file to be uploaded "; break; case '2': $ res ['Mes '] = "exceeds the size of the file to be uploaded in the form setting"; break; case '3 ': $ res ['Mes '] = "partially uploaded files"; break; case '4': $ res ['Mes'] = "no files uploaded"; break; case '6': $ res ['Mes '] = "no temporary directory found"; break; case '7 ': $ res ['Mes '] = "file cannot be written"; break; default: $ res ['Mes'] = "file Upload failed"; break ;}} return $ res ;}

Two small functions are involved:

/*** Get the file extension * @ param string $ filename Upload file name * @ return string returns the extension */function getExt ($ filename) {$ arr = explode ('. ', basename ($ filename); return end ($ arr );} /*** get the unique file extension * @ return string after md5, generate a 32-bit unique Upload file name */function getUniName () {return md5 (uniqid (microtime (true ), true ));}

Feelings:

I had access to PHP file upload a long time ago, and I felt confused. Now it seems that as long as you master the information contained in $ _ FILES, you can use the knowledge you have written to process some tips. The system has logical considerations and encapsulates functions in a timely manner, in the future, files can be uploaded quickly. The code I presented this time cannot meet any needs, so you can transform it into your own code. For example, if you want to upload a file as an image content, the extension alone is absolutely unknown. you also need to use the image features for verification.

  

Summary, before talking nonsense, first declare that this article is based on understanding the php single file upload, so here we will not go into details about the file upload server configuration, table...

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.