PHP File Upload

Source: Internet
Author: User

File upload the overall idea is: first to determine whether the file format and size to meet the requirements, and then determine if the file name is the same, in this middle we also need to set the encoding format according to the difference between the format of the file, when the file formats, size, file name is not the same, to allow the file to be saved to the appropriate directory

When using PHP for file upload, we need to first establish a form form, using form form one-way server to send related books, the code is as follows:

<form action= "chuli.php" method= "post" enctype= "Multipart/form-data" >//here. The form form is used to upload a file, so we need to add an extra attribute: The Enctype.enctype property specifies how the form data should be encoded before it is sent to the server. Here we do is the file upload, so we choose Multipart/form-data    <input type= "file" name= "file"/>//in the input tag, we were connected to the database, When working with the database, we set the Type property to implement different functions, such as text boxes, password boxes, text fields, and so on. Here, we set the Type property to file so that it can be uploaded    <input type= "Submit" value= "Upload"/></form>

In front of the upload operation completed, we need to submit to the background for processing, such as to determine whether to upload the file exists, whether the system supports the file format, encountered the name of the file is how to do, etc., the code is as follows:

<?phpvar_dump ($_files["file"]);//In the previous exercise, we used most of the post method to obtain data, but here we use FILES. Here, we can use Var_dump ($_files["file"]) to view some information about the file to be uploaded, such as file size, file format, filename, etc.
In general, we need to pay special attention to the following 4 questions when uploading a file://1. Control the type and size of the uploaded file//2. If the file name repeats, overwrite the//3. The probability of reducing the names of files modify the filename to build the folder//4. if (($_files[" File "[" type "] = =" Image/jpeg "| | $_files["File" ["type"] = = "Image/png") && $_files["File" ["Size"]<=102400) {//We are based on $_files["file"] Array to obtain the file data, determine whether the file type conforms to the requirements, as to the format of the file, we have been obtained before, so the specific conditions of writing such as the above code//upload operation//Create a storage path $yuan = $_files["File" ["Tmp_name" ];//we used Var_dump ($_files["file"])//To view the file information, we see a tmp_name, which shows the file upload process, the file's temporary storage path $filename = "./img/". Date ("Ymdhis"). $_files["File" ["Name"];//to prevent duplicate names, we add a timestamp to the file name, accurate to the second. The encoding format of the storage path is changed to GB $filename = Iconv ("UTF-8", "gb2312", $filename);//The ICONV function is used when setting the encoding format of the file, from left to right it requires three parameters, Respectively: the current encoding format of the file, the converted encoding format, the file name, the three are separated by commas//To determine whether the file exists if (file_exists ($filename))//file_exists used to check the file or directory exists {die (" File already exists ");} Move File Move_uploaded_file ($yuan, $filename);
When the file meets the requirements of the set, will be transferred from the training store path to a set of storage directory, in this process, the need to move_uploaded_file function, it has two parameters: the file name of the upload file, to move to the location}else{echo " The uploaded file does not meet the requirements ";}? >

  

PHP File Upload

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.