- //Encapsulation of single file upload function
- //Principle of file upload: Upload client files to the server, and then move the temporary files on the server to the specified directory.
- //Direction of the file: Client -> Server (temporary file) -> Specify the directory. When the file enters the server, it becomes a temporary file. At this time, the name of the temporary file tmp_name should be used in the operation.
- //It is unsafe to set upload file restrictions (file type and size) on the client because customers can modify the restrictions through source code, so set restrictions here on the server.
- //Set encoding to UTF-8 to avoid Chinese garbled characters
- header('Content-Type: text/html;charset=utf-8');
- //Receive uploaded file information through $_FILES
- $fileInfo = $ _FILES['myFile'];
- function uploadFile($fileInfo,$uploadPath='uploads',$flag=true,$allowExt=array('jpeg','jpg','png','gif'),$maxSize = 2097152){
- //Judge the error number, it can only be 0 or UPLOAD_ERR_OK, no error occurs, the upload is successful
- if($fileInfo['error']>0){
- //Attention! The error message is no 5
- switch($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= 'Exceeded the size of the HTML form MAX_FILE_SIZE limit';
- break;
- case 3:
- $mes= 'The file was partially uploaded';
- break;
- case 4:
- $mes= 'No option to upload the file';
- break;
- case 6:
- $mes= 'Temporary directory not found';
- break;
- case 7:
- $mes= 'File writing failed';
- break;
- case 8:
- $mes= 'The uploaded file was deleted by PHP Extension program break';
- break;
-
- }
- exit($mes);
- }
- $ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION);
- //$allowExt=array('jpeg',' jpg','png','gif');
-
- //Detect the type of uploaded file
- if(in_array($ext,$allowExt)){
- exit('illegal file type');
- }
-
- // Check whether the file size of the uploaded file meets the specification
- //$maxSize = 2097152;//2M
- if($fileInfo['size']>$maxSize){
- exit('The uploaded file is too large');
- }
-
- //Check whether the picture is a real picture type
- //$flag=true;
- if($flag){
- if(!getimagesize($fileInfo['tmp_name'])){
- exit('not a real picture Type');
- }
- }
-
- //Check whether it is uploaded through HTTP POST
- if(!is_uploaded_file($fileInfo['tmp_name'])){
- exit('The file is not uploaded through HTTP POST ');
- }
-
- //$uploadPath='uploads';
- //If there is no such folder, create one
- 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;
- //The @ symbol is to prevent customers from seeing error messages
- if(! @move_uploaded_file($fileInfo['tmp_name'], $destination )){
- exit('File move failed') ;
- }
-
- //echo 'File uploaded successfully';
- //return array(
- // 'newName'=>$destination,
- // 'size'=>$fileInfo['size'],
- // 'type'=>$fileInfo['type']
- //);
- return $destination;
- }
- ?>
-
Copy code
|