PHP File Upload

Source: Internet
Author: User
Tags php file upload temporary file storage

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 uploaded file
    • $_files["File" ["Type"]-type of upload file
    • $_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

This is a very simple way to upload files. For security reasons, you should increase the restrictions on which users are allowed to upload files.

Upload limit

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

<?PhpAllow uploading of picture suffixes$allowedExts=Array("GIF", "JPEG", "JPG", "PNG");$temp=Explode(".",$_files["File"]["Name"]);$extension= End($temp); Get file suffix nameIf ((($_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"] < 204800) Less than KB&&In_array($extension,$allowedExts)){If ($_files["File"]["Error"] > 0){Echo"Error::" .$_files["File"]["Error"] . "<br>";}Else{Echo"Upload file name:" .$_files["File"]["Name"] . "<br>";Echo"File type:" .$_files["File"]["Type"] . "<br>";Echo"File Size:" . ($_files["File"]["Size"] / 1024)    "kb<br>" ; "File temporary storage location:"  .[ "file" ][ "Tmp_name" }}else {echo  "illegal file format" ;}?>       
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:

<?PhpAllow uploading of picture suffixes$allowedExts=Array("GIF", "JPEG", "JPG", "PNG");$temp=Explode(".",$_files["File"]["Name"]);Echo $_files["File"]["Size"];$extension= End($temp); Get file suffix nameIf ((($_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"] < 204800) Less than KB&&In_array($extension,$allowedExts)){If ($_files["File"]["Error"] > 0){Echo"Error::" .$_files["File"]["Error"] . "<br>";}Else{Echo"Upload file name:" .$_files["File"]["Name"] . "<br>";Echo"File type:" .$_files["File"]["Type"] . "<br>";Echo"File Size:" . ($_files["File"]["Size"] / 1024) . "Kb<br>";Echo"Location of temporary file storage:" .$_files["File"]["Tmp_name"] . "<br>";Determine if the file exists in the upload directory under the current directoryIf there is no upload directory, you need to create it, upload directory permission is 777If (File_exists("Upload/" .$_files["File"]["Name"])){Echo $_files["File"]["Name"] . "The file already exists. ";}Else{If the file is not present in the upload directory, upload the file to the upload directoryMove_uploaded_file($_files["File"]["Tmp_name"], "Upload/" .$_files[ "file" ][ "name" ); Span class= "PLN" >echo  "files are stored in:"  .  "upload/"  . $_files[< span class= "str" > "file" ][ "name" ];}}}< Span class= "KWD" >else{echo  "illegal file format" Span class= "pun" >; }?>         

The script above detects if the file already exists, and if it does not, copies the file to a directory named "Upload".

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.