PHP File Upload

Source: Internet
Author: User

As is known to all, file uploads are an essential part of some website applications. such as personal blog upload personality Avatar, some forums to share good learning materials, which involves the use of the form to process file upload knowledge, in PHP we can use $_files this global array to handle. Here's a look at the basic File upload Processing section.

0x01 Create a File upload form

 enctype for

Some of the considerations for the above HTML form are listed below:

    • The enctype attribute of the <form> tag specifies what type of content to use when submitting a form. Use "Multipart/form-data" when the form requires binary data, such as the contents of a file.
    • The type= "file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

0x02 Creating an upload script

<?PHPif($_files["File"] ["error"] > 0) { Echo"Error:".$_files["File"] ["Error"]. "<br>"; } Else { Echo"Upload:".$_files["File"] ["Name"]. "<br>"; Echo"Type:".$_files["File"] ["Type"]. "<br>"; Echo"Size:". ($_files["File"] ["Size"]/1024). "<br>"; Echo"Stored in:".$_files["File"] ["Tmp_name"]; } ?>

By using PHP's global array $_files, you can upload files from a client computer to a remote server.

The first parameter is the input name of the form, and the second subscript can be "name", "type", "Size", "tmp_name", or "error". As shown below:

    • $_files["File" ["Name"]-the name of the file being uploaded
    • $_files["File" ["type"]-the type of file being uploaded
    • $_files["File" ["Size"]-the size of the uploaded file, measured in bytes
    • $_files["File" ["Tmp_name"]-the name of the temporary copy of the file stored on the server
    • $_files["File" ["Error"]-error code caused by file upload

$_files[["Error"] value in "File" ["Error"]

0: No error occurred, file upload succeeded 1: Uploaded file exceeded php. INI in upload_max_filesize (2M by default) option limit value 2: The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form 3: only partially uploaded files 4: No file is uploaded 5: File size is 0

0x03 Upload Limit

In this script, we have added restrictions on file uploads. Users can only upload. gif and. jpeg files, and the file size must be less than KB:

Allowedexts =Array("GIF", "JPEG", "JPG", "PNG"); $temp=Explode(".",$_files["File"] ["Name"]); Break the string into groups. $temp[0]== file name here, $temp [1]== extension$extension=End($temp); Points the pointer inside the array to the last element and returns the value of the element. The extension is returned hereif((($_files["File"] ["type"] = = "Image/gif") || ($_files["File"] ["type"] = = "Image/jpeg") || ($_files["File"] ["type"] = = "Image/jpg")|| ($_files["File"] ["type"] = = "Image/pjpeg")|| ($_files["File"] ["type"] = = "Image/x-png") || ($_files["File"] ["type"] = = "Image/png")) && ($_files["File"] ["Size"] < 20000) &&In_array($extension,$allowedExts)//To determine if the extension is in the array $allowedexts {if($_files["File"] ["error"] > 0) { Echo"Error:".$_files["File"] ["Error"]. "<br>"; } Else { Echo"Upload:".$_files["File"] ["Name"]. "<br>"; Echo"Type:".$_files["File"] ["Type"]. "<br>"; Echo"Size:". ($_files["File"] ["Size"]/1024). "<br>"; Echo"Stored in:".$_files["File"] ["Tmp_name"]; } } Else { Echo"Invalid file"; } ?>

0x04 Save the uploaded file

The above example creates a temporary copy of the uploaded file in the server's PHP Temp folder.

This temporary copy file disappears at the end of the script. To save the uploaded file, we need to copy it to another location:

<?PHP
/* Upload limit * /$allowedExts=Array("GIF", "JPEG", "JPG", "PNG"); $temp=Explode(".",$_files["File"] ["Name"]);$extension=End($temp); if((($_files["File"] ["type"] = = "Image/gif") || ($_files["File"] ["type"] = = "Image/jpeg") || ($_files["File"] ["type"] = = "Image/jpg")|| ($_files["File"] ["type"] = = "Image/pjpeg")|| ($_files["File"] ["type"] = = "Image/x-png") || ($_files["File"] ["type"] = = "Image/png")) && ($_files["File"] ["Size"] < 20000) &&In_array($extension,$allowedExts)) { if($_files["File"] ["error"] > 0) { Echo"Return Code:".$_files["File"] ["Error"]. "<br>"; } Else { Echo"Upload:".$_files["File"] ["Name"]. "<br>"; Echo"Type:".$_files["File"] ["Type"]. "<br>"; Echo"Size:". ($_files["File"] ["Size"]/1024). "<br>"; Echo"Temp file:".$_files["File"] ["Tmp_name"]. "<br>";
/* Save the Upload file * /if(file_exists("upload/".$_files["File"] ["Name"])) { Echo $_files["File"] ["Name"]. "already exists."; } Else { Move_uploaded_file($_files["File"] ["Tmp_name"], "upload/".$_files["File"] ["Name"]); Move the upload file to the upload directoryEcho"Stored in:". Upload/".$_files["File"] ["Name"]; } } } Else { Echo"Invalid file"; } ?>

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.