PHP Upload principle and implementation

Source: Internet
Author: User
Tags form post
About the upload principle and simple upload operation:



1. Form Tag Enctype Property

The enctype= "Multipart/form-data" in the form is the MIME encoding used to set up the form.
By default, this encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;
Only the multipart/form-data is used and the submission method is post for full file data transfer.

2. max_file_size Hidden Fields

Max_file_size hidden Fields (in bytes) must precede the file input field with a value of the maximum size of the received file. This is a suggestion for the browser, PHP will also check this.
This setting can be bypassed easily on the browser side, so do not expect this feature to block large files. (However, it is best to add this item to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too large to upload.) )

upload.php Print_r ($_files);? >array (
[File] = Array
(
[Name] = Image.jpg
[Type] = Image/jpeg
[Tmp_name] = F:\wamp\tmp\php41BB.tmp
[ERROR] = 0
[Size] = 73886
)

)



3. Application of global variable $_files

$_files[' file ' [' Name ']; To upload the file's original filename
$_files[' file ' [' type ']; MIME type for uploading files
$_files[' file ' [' Size ']; The size of the uploaded file, in bytes
$_files[' file ' [' Tmp_name ']; Temporary file names stored on the server after the files have been uploaded ()
$_files[' file ' [' Error ']; Error code for File upload



4, by default, the upload file will be saved in the server side of the temporary folder, its directory in the php.ini set

PHP.ini some common settings related to file uploads:

File_uploads; Whether to allow the switch to upload files over HTTP.  The default is on, which is open upload_tmp_dir;  Files uploaded to the server where temporary files are stored, if not specified, the system will be the default temporary folder Upload_max_filesize; That is, the maximum size allowed for uploading files.  The default is 2mpost_max_size; Refers to the maximum value that can be received by a form post to PHP, including all values in the form. Default is 8M


Simple code:


Get upload file information
$fileName =$_files[' file ' [' Name ']; $fileType =$_files[' file ' [' type ']; $fileError =$_files[' file ' [' Error ']; $fileSize =$_files[' file ' [' Size ']; $tempName =$_files[' file ' [' tmp_name '];//temporary file name

Define the upload file type
$typeList = Array ("Image/jpeg", "image/jpg", "Image/png", "image/gif"); Define the allowed types

if ($fileError >0) {//Upload file error number judgment
Switch ($fileError) {Case 1: $message = "The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini. ";
Break Case 2: $message = "The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form. ";
Break Case 3: $message = "The file is only partially uploaded. ";
Break Case 4: $message = "No files were uploaded. "; Break Case 6: $message = "The temporary folder cannot be found. ";
Break Case 7: $message = "file Write Failed";
Break Case 8: $message = "File upload interrupted due to PHP extension"; Break
} exit ("File upload failed:". $message);

} if (!is_uploaded_file ($tempName)) {//To determine if the file was uploaded by post
Exit ("not uploaded by HTTP POST");
}else{if (!in_array ($fileType, $typeList)) {exit ("The uploaded file is not of the specified type");
}else{if (!getimagesize ($tempName)) {//Prevent users from uploading malicious files, such as changing the virus file name extension to picture format
Exit ("The uploaded file is not a picture");
}
if ($fileSize >100000) {//Limit the size of the upload file for a particular form
Exit ("Upload file exceeds the limit size");
}else{//Avoid uploading files in Chinese name garbled
$fileName =iconv ("UTF-8", "GBK", $fileName);//Convert the character encoding Iconv crawled from utf-8 to GBK output
$fileName =str_replace (".", Time (). ".", $fileName);//Add a timestamp after the name of the picture to avoid overwriting files with duplicate names
if (Move_uploaded_file ($tempName, "uploads/". $fileName) {echo "uploads the file successfully!) ";
}else{echo "Upload file failed";
}
}

}?>



5, about the PHP upload file some common functions:

File_exists ()//check whether the file or directory exists Is_uploaded_file ()//Determine if the file is uploaded via HTTP POST move_uploaded_file ()//Move the uploaded file to a new location Is_writab Le ()//Determine if the given file name is writable Iconv ()//character encoding str_replace ()//String substitution (change filename, anti-duplicate name) getimagesize ()// Check whether it is a picture file (other types of files can be detected even if the suffix name is changed)

  • 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.