PHP Simple File Upload $_files and move_uploaded_file implementation file upload

Source: Internet
Author: User
Tags error code file upload html form http post php tutorial save file

PHP Tutorial Simple File Upload $_files and move_uploaded_file implementation file upload

$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["Type"]-type of file being uploaded
$_files["File" ["Size"]-the size of the file being uploaded, in bytes
$_files["File" ["Tmp_name"]-name of the temporary copy of the file stored on the server
$_files["File" ["Error"]-error code caused by file upload
This is a very simple way to upload files. For security reasons, you should increase the restrictions on what users have permission to upload files.

Note: For IE, the type of recognition JPG file must be pjpeg, for Firefox, must be JPEG.

/*
* $formname HTML Upload form name
* $allowtype _array An array of file types allowed to be uploaded
* $allow _size allowed to upload the maximum size of the file, the default is 1MB
* $isrename Whether to rename the uploaded file
*/
function Upload_file ($formname, $path, $allowtype _array, $allow _size= ' 1048576 ', $isrename =true) {
if ($_files[$formname] [' tmp_name ']== '] | | $_files[$formname [' name ']== ' | | $_files[$formname] [' size ']==0] {
return false;
}
if (!file_exists) ('.. /uploadfiles/'. $path)) {
if (!mkdir) ('.. /uploadfiles/'. $path, 0777)) {
Writeerrmsg (' Create upload file Save file directory failed, please contact admin to check directory permissions ');
}
}
if ($_files[$formname] [' Size ']> $allow _size) {
Writeerrmsg (' Upload file is too large, please limit the upload file to '. Number_format ($allow _size/1048576, ' 2 ', '. ', '). ' MB within ");
}
$file _name_array=explode ('. ', $_files[$formname] [' name ']);
$file _type=strtolower ($file _name_array[count ($file _name_array)-1]);
if (!in_array ($file _type, $allowtype _array)) {
Writeerrmsg (' Upload file type error, only allow upload suffix named. ') Implode ('. ', $allowtype _array). ' Document ");
}
$save _name= $isrename randomname (). $file _type:$_files[$formname] [' name '];
if (!move_uploaded_file ($_files[$formname] [' tmp_name '], '. /uploadfiles/'. $path. ' /'. $save _name)) {
Writeerrmsg (' error occurred during file upload, please upload ');
}
Switch ($_files[$formname] [' ERROR ']) {
Case 0:
return $save _name;
Break
Case 1:
Writeerrmsg (' uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini ');
Break
Case 2:
Writeerrmsg (' The size of the uploaded file exceeds the value specified in the MAX_FILE_SIZE option in the HTML form ');
Break
Case 3:
Writeerrmsg (' files are only partially uploaded ');
Break
Case 4:
Writeerrmsg (' No files uploaded ');
Break
Default
Writeerrmsg (' can appear this error hint, the day also should mix the head ");
Break
}
}

Move_uploaded_file
This function checks to make sure that files specified by file are valid uploads (that is, uploaded via Php's http post upload mechanism). If the file is valid, it is moved to the file specified by Newloc.

If file is not a legitimate upload file, no action occurs, Move_uploaded_file () returns false.

If file is a legitimate upload, but cannot be moved for some reason, Move_uploaded_file () will return false and a warning will be issued.

This check is particularly important if the uploaded file is likely to cause the content to be displayed to users or other users of the system

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.