PHP file upload-single file upload, _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Php file upload: single file upload ,. Single file upload for php file upload. for simplicity, php file and form are written in a file. php single file Upload ---- 1! DOCTYPEhtml2html3head4metacharsetUTF-8 php file Upload single file upload,

For simplicity, php files and form forms are written in a file.

Php single file Upload ---->

1234
 567 121314 15 16 17
 0) {24 switch ($ fileInfo ['error']) {25 case $ msg_error = 'the uploaded file exceeds the value of the UPLOAD_MAX_FILESIZE option in the php configuration file '; 27 break; 28 case $ msg_error = 'size beyond the size limit of form MAX_FILE_SIZE '; 30 break; 31 case $ msg_error = 'partial file upload'; 33 break; 34 case 4: 35 $ msg_error = 'No file upload'; 36 break; 37 case 6: 38 $ msg_error = 'no temporary directory found '; 39 break; 40 case 7:41 case 8:42 $ msg_error = 'system error'; 43 break; 44} 45 exit ($ msg_error); 46} 47 $ filename = $ fileInfo ['name' ]; 48 // get the file extension 49 $ ext = strtolower (substr ($ filename, strrpos ($ filename ,'. ') + 1); 50 // defines the file extensions that can be uploaded. 51 $ allowExt = array ('txt', 'HTML ', 'PNG', 'GIF ', 'jpeg '); 52 // check the type of the uploaded file 53 if (! In_array ($ ext, $ allowExt) {54 exit ('upload file type error'); 55} 56 57 58 // check the file size 59 $ maxSize = 2097152; 60 if ($ fileInfo ['size']> $ maxSize) {61 exit ('upload file oversized '); 62} 63 64 // check whether the 65 if (! Is_uploaded_file ($ fileInfo ['tmp _ name']) {66 exit ('file not submitted through http post '); 67} 68 69 // ensure the file name is unique and prevent files with the same name from being overwritten by 70 $ uniqName = md5 (uniqid (microtime (true), true )). '. '. $ ext; 71 72 // defines the folder to store. if this folder is not available, create 73 $ path = 'uploads'; 74 if (! File_exists ($ path) {75 mkdir ($ path, 0777, true); 76 chmod ($ path, 0777); 77} 78 $ destination = $ path. '/'. $ uniqName; 79 80 // move the file to the directory 81 to be saved if (! @ Move_uploaded_file ($ fileInfo ['tmp _ name'], $ destination) {82 exit ('file Upload failed'); 83} 84 85 echo 'uploaded successfully '; 86 87} 88?>

For simplicity, the php file and form are written in a single file. php single file Upload ---- 1! DOCTYPE html 2 html 3 head 4 meta charset = "UTF-8 "...

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.