- Encapsulation of single-file upload functions
- File Upload principle: To upload the client's files to the server side, and then move the server-side temporary files to the specified directory.
- The direction of the file: client-to-server (temporary file)--The specified directory, when the file enters the server it is a temporary file, the operation is to use the name of the temporary file tmp_name.
- It is not safe to set the limit (file type and size) of the upload file on the client, because the customer can modify the limit by the source code, so set the limit here on the server side.
- Set the encoding to UTF-8 to avoid Chinese garbled characters
- Header (' Content-type:text/html;charset=utf-8 ');
- Receive information about uploading files via $_files
- $fileInfo = $_files[' myFile ');
- function UploadFile ($fileInfo, $uploadPath = ' uploads ', $flag =true, $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), $ MaxSize = 2097152) {
- Determine the error number, only for 0 or UPLOAD_ERR_OK, no error occurred, upload successful
- if ($fileInfo [' Error ']>0) {
- Attention! Error message No 5
- Switch ($fileInfo [' ERROR ']) {
- Case 1:
- $mes = ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file ';
- Break
- Case 2:
- $mes = ' exceeds the size of the HTML form max_file_size limit ';
- Break
- Case 3:
- $mes = ' file part is uploaded ';
- Break
- Case 4:
- $mes = ' no option to upload file ';
- Break
- Case 6:
- $mes = ' No temporary directory found ';
- Break
- Case 7:
- $mes = ' file write Failed ';
- Break
- Case 8:
- $mes = ' uploaded file is interrupted by PHP extension ';
- Break
- }
- Exit ($mes);
- }
- $ext =pathinfo ($fileInfo [' name '],pathinfo_extension);
- $ALLOWEXT =array (' jpeg ', ' jpg ', ' png ', ' gif ');
- Detecting the type of upload file
- if (In_array ($ext, $allowExt)) {
- Exit (' Illegal file type ');
- }
- Detects if the size of the uploaded text conforms to the specification
- $maxSize = 2097152;//2m
- if ($fileInfo [' Size ']> $maxSize) {
- Exit (' Upload file too large ');
- }
- Detect if a picture is a real picture type
- $flag =true;
- if ($flag) {
- if (!getimagesize ($fileInfo [' tmp_name '])) {
- Exit (' Not a real picture type ');
- }
- }
- Detection is uploaded via HTTP post
- if (!is_uploaded_file ($fileInfo [' tmp_name '])) {
- Exit (' File not uploaded by HTTP post ');
- }
- $uploadPath = ' uploads ';
- If you do not have this folder, create a
- if (!file_exists ($uploadPath)) {
- mkdir ($uploadPath, 0777, true);
- chmod ($uploadPath, 0777);
- }
- New file name unique
- $uniName = MD5 (Uniqid (Microtime (True), true)). '. '. $ext;
- $destination = $uploadPath. ' /'. $uniName;
- The @ sign is to keep the customer from seeing the error message
- if (! @move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {
- Exit (' File move failed ');
- }
- echo ' file upload succeeded ';
- Return Array (
- ' NewName ' = $destination,
- ' Size ' = $fileInfo [' Size '],
- ' Type ' = $fileInfo [' type ']
- //);
- return $destination;
- }
- ?>
Copy Code
|