Php uploads and downloads files ). Php implements file upload and download (in). If you do not want to see the information, you can use the error suppression symbol @. of course, you can assign values to variables for echo; define using php to upload and download files (medium)
You can use the error suppression symbol @ to display information that you do not want to see. of course, you can assign values to a variable for echo;
Define the type of the uploaded file, put it in the array variable allowExt, and use if (! In_array (the first parameter is the suffix of the uploaded file. you can use the strtolower function to determine the dot '. ', or you can use the built-in extension function pathinfo (file name, PATHINFO_EXTENSION) to determine. The second parameter is the array variable allowExt of the type that can be uploaded ); however, in this case, if a user uploads a virus information, but changes the suffix, getimagesize is required to obtain the image information. The basic information includes the width, height, and type of the image, some channels and bits are also available. if you are allowed to upload more than just the image type, use the boolean value of flag for determination;
Note that the name of the file uploaded by the user cannot be overwritten by the same name. md5 (uniqid (microtime (true), true) or similar functions can be used for determination;
You can use if (! Files_exit (path). if it does not exist, use mkdir ($ path, 0777, true) to customize the upload path;
After various judgments, you can encapsulate this file into a function, set the variables used in the following text to the parameters of the function, and pass the parameters during the call, you can directly call the function when uploading multiple files in a single file, greatly simplifying the amount of code;
The basic idea of uploading a single file is to first check the error information and match them one by one. use switch ...... Case output error information ------ check whether the file upload type is one of the defined arrays ----- check whether the uploaded file meets the size requirements in the specification ---- check whether the file image and other content are real image types (use flag and getimagesize to obtain information) ---- check whether the file is uploaded using the http post method ---- define the name and path of the file to be uploaded (in this case, check whether the path exists and whether the file with the same name exists, and rename it using encryption) --- "indicates that the upload is successful. a specific array variable or file name is returned.
1
$ MaxSize) {24 exit ('upload file is too large '); 25} 26 // Determine the file type: two methods: determine 27 $ ext = pathinfo ($ fileInfo ['name'], PATHINFO_EXTENSION) by intercepting the suffix or using the extension of your own ); 28 $ ext = strtolower (end (explode ('. ', $ fileInfo ['name']); 29 if (! In_array ($ ext, $ allowExt) {30 exit ('invalid file type'); 31} 32 // Determine whether the file is put on 33 if (! Is_uploaded_file ($ fileInfo ['tmp _ name']) {34 exit ('file is not passed through the http post method '); 35} 36 if ($ flag) {37 if (! Getimagesize ($ fileInfo ['tmp _ name']) {38 exit ('not a real image type'); 39} 40} 41 // upload a file, first, determine whether the path exists. if not, create 42 // $ path = 'text'; 43 if (! File_exists ($ path) {44 mkdir ($ path, 0777, true); 45 chmod ($ path, 0777); 46} 47 48 // ensure that the file name is unique, to prevent duplicate names from overwriting, use the encryption function md5, mcrotime indicates the number of microseconds, and the two parameters are true; 49 $ uniName = md5 (uniqid (microtime (true), true )). '. '. $ ext; 50 // test whether echo $ uniName; 51 // exit (); 52 $ destination = $ path. '/'. $ uniName; 53 if (move_uploaded_file ($ fileInfo ['tmp _ name'], $ destination) {54 // echo 'uploaded successfully'; not only indicates that the return is successful, 55 // return array (56 // 'newname' => $ destination, 57 // 'size' => $ fileInfo ['size'], 58 // 'type' => $ fileInfo ['type'] 59 //); 60 return $ destination; 61 // If you only need a file name, return $ destination: 62} else {63 echo 'upload failed'; 64} 65} else {66 // matching error message, the one-to-one switch ($ fileInfo ['error']) that corresponds to the err number {68 case 1: 69 $ remindMes = 'upload file exceeds the value of the upload_max_filesize option in the php configuration file '; 70 break; 71 case 2: 72 $ remindMes = 'upload file exceeds the value of the max_file_size option of the form '; 73 break; 74 case 3: 75 $ remindMes = 'upload part of file'; 76 break; 77 case 4: 78 $ remindMes = 'file not selected'; 79 break; 80 case 6: 81 $ remindMes = 'temporary directory not found '; 82 break; 83 case 7: 84 85 case 8: 86 $ remindMes = 'system error'; 87 break; 88} 89 exit ($ remindMes); 90} 91}
Contains (medium) information that you do not want to see. you can use the error suppression symbol @. of course, you can assign values to a variable for echo. defined...