PHP implementation file upload and download (medium) _php tutorial

Source: Internet
Author: User

PHP implementation file upload and download (medium)


If you do not want to let the user see the information, you can use the error suppression symbol @; Of course, the echo can be assigned to a variable; define the user upload file type, put it in the array variable allowext, with if (!in_array (the first parameter is to get the suffix name of the uploaded file, You can use the Strtolower function to determine the point number '. ' After the suffix name, or you can also use the extension function PATHINFO (file name, pathinfo_extension) to determine, the second parameter is allowed to upload the type of the array variable allowext); But in this case, if the user uploads a virus message , just changed the suffix name, it is necessary to use the getimagesize to obtain the information of the picture, the basic information will have the width and height of the picture and the type, some will also have channel and bits; If the user is allowed to upload more than the image type, then the Boolean value of flag is used to determine; Also note is the user to upload the name of the file, can not be overwritten with duplicate names, may use MD5 (Uniqid (Microtime (True), true) or similar functions to determine, sometimes users need to upload to their own directory, you can use if (! Files_exit (path) to determine, does not exist in the mkdir ($path, 0777,true) to customize the path of the upload, after a variety of decisions, the file can be encapsulated into a function, the following will be used to set the variable to the function's parameters, In the call can be passed the parameter, in a single file multi-file upload can be directly called functions, greatly simplifying the amount of code; Single File upload the basic idea is: first detect the error message, one match, with switch......case output corresponding error message------" Detects whether the type of the file upload is one of its own defined array-----"detects if the upload file meets the size requirements in the specification----" whether the content of the test file picture is a real picture type (with flag and getimagesize)----" Detect whether the file is passed through the HTTP POST method----"Definition file upload the name and path (need to detect the existence of the path and the name of the file exists, encrypted rename)---" Upload successful, return the specific array variable or upload the file name.
1
  $maxSize) {$ exit (' upload file too large '); 25}26//Determine file type: Two methods--using the Intercept suffix name or the decision with the extension to determine the $ext = PA         Thinfo ($fileInfo [' name '], pathinfo_extension), $ext = Strtolower (End (Explode ('. ', $fileInfo [' name '])); 29         if (! In_array ($ext, $allowExt)) {exit (' illegal file type '); 31}32//Determine if the file has been put on by HTTP POST 33 if (! is_uploaded_file ($fileInfo [' Tmp_name '])} {The ' file is not passed through the HTTP POST method ');}36 if ($flag)         {getimagesize ($fileInfo [' tmp_name ']) {$ exit (' not a true picture type '); 39}40 }41//Upload a file, first determine whether the path exists, if not exist then establish a//$path = ' text '; if (! file_exists ($path)) {m Kdir ($path, 0777, True); chmod ($path, 0777); 46}47 48//To ensure the file name is unique, prevent overwriting with duplicate names, use cryptographic functions MD5 , Mcrotime represents the number of microseconds, and two parameters are true;49 $uniName = MD5 (Uniqid (Microtime (True), true)). '.' . $ext; 50//test can be encrypted output echo $uniName; 51//exit (); $destination = $path. '/' . $uniName, if (Move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {si//echo ' upload succeeded '; Can not only show back Successful, can return the information content of the file, etc.//return Array (NewName//' + ' = $destination, +//' Si Ze ' = = $fileInfo [' Size '],58/' type ' = ' = ' = ' type ']59//);      $destination; 61//If only one file name, then return $destination can be A +} else {echo ' upload failed '; 64}65                 } else {66//Match error message, and the ERR number should be one by one corresponding to the ($fileInfo [' ERROR ']) {1:69                $remindMes = ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file '; break;71 case 2:72 $remindMes = ' upload file exceeds the value of the form max_file_size option '; break;74 case 3:75 $remindMes = ' text break;77 case 4:78 $remindMes = ' NoSelect Upload file '; break;80 case 6:81 $remindMes = ' No temporary directory found '; ; 7:84 case 8:86 $remindMes = ' system error '; ;}89 exit ($remindMes); 90}91}


http://www.bkjia.com/PHPjc/1066541.html www.bkjia.com true http://www.bkjia.com/PHPjc/1066541.html techarticle PHP implementation file upload and download (in) the information that you do not want the user to see, you can use the error suppression symbol @; Of course, anything that echoes can be assigned to a variable;

  • 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.