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