PHP multiple File Upload

Source: Internet
Author: User
Tags http post

Key functions:

Is_uploaded_file (): Used to determine whether the specified file was uploaded via HTTP POST, or TRUE if it is. Used to prevent potential attackers from illegally managing files that could not otherwise be scripted, which can be used to ensure that malicious users cannot spoof scripts to access files that they cannot access.

Move_uploaded_file (): After the file is uploaded, it is first stored in the server's temp directory, which can be the function to move the uploaded file to a new location. Compared to copy () and move (), the function has an additional function of checking and ensuring that the file specified by the first parameter, filename, is a valid upload file (that is, uploaded via PHP's HTTP POST upload mechanism). If the file is valid, move it to the file specified by the second parameter, destination. If filename is not a valid upload file, no action will be returned, FALSE. If filename is a legitimate upload file, but for some reason it cannot be moved, no action will occur, and false is returned. A warning will also be issued. Returns true if successful.

Array_pop (): pops up and returns the last element in the array, reducing the length of the array by 1. The case is used to remove the file suffix.

List (): Assigns a value to a set of variables using the elements in the array.

Multiple file uploads only need to be performed on a single file upload basis, in addition to the original two-dimensional array $_files.

Html:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>File Upload</title></Head><Body>    <formAction= "upload.php"Method= "POST"enctype= "Multipart/form-data">                <inputtype= "hidden"name= "Max_file_size"value= "1000000">Select file 1:<inputtype= "File"name= "myfile[]"><BR>Select File 2:<inputtype= "File"name= "myfile[]"><BR>Select File 3:<inputtype= "File"name= "myfile[]"><BR>        <inputtype= "Submit"value= "Upload file">    </form></Body></HTML>

Figure 1

Figure 2

uoload.php:

<?PHP$allowtype=Array("GIF", "png", "JPG");//allow upload of file formats$size= 1000000;//set allowed files within 1M size$path= "./uploads";//set the path to save the file after uploading//determine if the file can be uploaded to the server $_files[' myfile '][error] 0 for Success //Loop for($i= 0;$i<Count($_files[' MyFile '] [' ERROR ']);$i++ ){    $upfile[$i] =$_files[' MyFile '] [' Name '] [$i]; if($_files[' MyFile '] [' ERROR '] [$i]>0){        Echo"Upload Error"; Switch($_files[' MyFile '] [' ERROR '] [$i]){                     Case1: die(' No. '. ($i+1). ' File upload file size exceeds the convention value in PHP config file: upload_max_filesize ');  Case2: die(' Upload section '. ($i+1). ' File size exceeds the contract value in the form: Max_file_size ');  Case3: die(' No. '. ($i+1). ' Files are only partially uploaded ');  Case4: die(' No. '. ($i+1). ' Files not uploaded '); default: die(' Unknown error '); }    }    //determines whether the uploaded file is a permitted file type, pops up through the file's suffix name//array_pop and returns the last element in the array, and the length of array minus 1    $hz[$i] =Array_pop(Explode(".",$_files[' MyFile '] [' Name '] [$i])); if(!In_array($hz[$i],$allowtype)){         die("No.". ($i+1). " A file suffix is <b>{$hz}</b>, not a file type allowed! "); }    /*You can also restrict file upload type list ($maintype, $subtype) = Explode ("/", $_files[' myfile ' [' type ']) by obtaining the main type and subtype in the MIME type of the uploaded file;    if ($maintype = = "Text") {die ("Cannot upload text file"); }    */    //determine if the uploaded file is allowed size    if($_files[' MyFile '] [' Size '] [$i]>$size){         die("No.". ($i+1). " File exceeds the allowable <b>{.$size}</b> "); }    //for system security, but also for files with the same name is not overwritten, after uploading the file name using the system definition    $filename[$i] =Date("Ymdhis").Rand(100,999). ".".$hz[$i]; //determine whether to upload files    if(Is_uploaded_file($_files[' MyFile '] [' Tmp_name '] [$i])){        if(!Move_uploaded_file($_files[' MyFile '] [' Tmp_name '] [$i],$path.‘ /‘.$filename[$i])){                     die("Cannot move files to the specified directory"); }    }Else{         die("Upload file {$_files[' MyFile '] [' Name '] [$i]} is not a valid file "); }    //if the file upload succeeds    $filesize[$i] =$_files[' MyFile '] [' Size '] [$i]/1024; EchoFile$upfile[$i]} uploaded successfully, saved in directory {$path}, the file size is {$filesize[$i]}kb<br> ";}

Page display:

File 87d11a09d24848fa3ac763fe.jpg uploaded successfully, saved in directory./uploads, file size is 34.60546875KB
File 0ef5e0fef0adab0f5c6008a2.jpg uploaded successfully, saved in directory./uploads, file size is 37.810546875KB
File 2dbda88c96801dd6f11f3617.jpg uploaded successfully, saved in directory./uploads, file size is 168.0478515625KB

PHP multiple File Upload

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.