PHP single file Upload principle and encapsulation of Upload functions _ PHP Tutorial

Source: Internet
Author: User
PHP single file Upload principle and encapsulation of the upload function. The principle of PHP single file upload and the encapsulation server (temporary file) of the upload function specify a directory. when the file enters the server, it is a temporary file, in this case, the temporary file name should be used in the operation. t. PHP single file Upload principle and the encapsulation of the upload function.
 Server (temporary file) --> specifies the directory. when the file enters the server, it is a temporary file. in this case, the temporary file name tmp_name must be used in the operation. // It is insecure to set the file upload restriction (file type and size) on the client, because the client can modify the restriction through the source code, so the restriction is set on the server. // Set the encoding to UTF-8 to avoid Chinese garbled header ('content-Type: text/html; charset = utf-8 '); // receives the information of the uploaded file through $ _ FILES $ fileInfo = $ _ FILES ['myfile']; function uploadFile ($ fileInfo, $ uploadPath = 'uploads ', $ flag = true, $ allowExt = array ('jpeg ', 'jpg', 'PNG ', 'GIF'), $ maxSize = 2097152) {// identify the error code, it is only 0 or UPLOAD_ERR_ OK, and no error occurs. if the upload is successful ($ fileInfo ['error']> 0) {// note! The error message does not contain 5 switches ($ fileInfo ['error']) {case 1: $ mes = 'the uploaded file exceeds the value of the upload_max_filesize option in the PHP configuration file'; break; case 2: $ mes = 'exceeds the size limit of the HTML form MAX_FILE_SIZE '; break; case 3: $ mes = 'File partially upload'; break; case 4: $ mes = 'No file upload'; break; case 6: $ mes = 'no temporary directory found '; break; case 7: $ mes = 'File write failed '; break; case 8: $ mes = 'files uploaded are interrupted by PHP extensions'; break;} exit ($ mes );} $ ext = pathinfo ($ fileInfo ['name'], PATHINFO_EXTENSION); // $ allowExt = array ('jpeg ', 'jpg', 'PNG ', 'G If '); // check the type of the uploaded file if (in_array ($ ext, $ allowExt) {exit ('invalid file type ');} // check whether the part size of the uploaded file complies with the specifications. // $ maxSize = 2097152; // 2Mif ($ fileInfo ['size']> $ maxSize) {exit ('upload file too large ');} // check whether the image type is true/$ flag = true; if ($ flag) {if (! Getimagesize ($ fileInfo ['tmp _ name']) {exit ('not a real image type');} // checks whether the image is uploaded through http post. if (! Is_uploaded_file ($ fileInfo ['tmp _ name']) {exit ('file is not uploaded using http post ');} // $ uploadPath = 'uploads '; // if this folder does not exist, create an if (! File_exists ($ uploadPath) {mkdir ($ uploadPath, 0777, true); chmod ($ uploadPath, 0777 );} // The new file name is unique $ uniName = md5 (uniqid (microtime (true), true )). '. '. $ ext; $ destination = $ uploadPath. '/'. $ uniName; // @ symbol is used to prevent the customer from seeing the error message if (! @ Move_uploaded_file ($ fileInfo ['tmp _ name'], $ destination) {exit ('file moving failed');} // echo 'file uploaded successfully '; // return array (// 'newname' => $ destination, // 'size' => $ fileInfo ['size'], // 'type' => $ fileInfo ['type'] //); return $ destination;}?>

The upload server (temporary file) specifies the directory. when the file enters the server, it is a temporary file. in this case, use the temporary file name t...

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.