Uploading of PHP files

Source: Internet
Author: User
Tags php file upload

One, $_files array
    • $_files["file" ["Name"]: The original name of the uploaded file.
    • $_files["file" ["Type"]: Uploaded file type.
    • $_files["file" ["Size"]: The original size of the uploaded file.
    • $_files["file" ["Tmp_name"]: The staging location after uploading the file.
    • $_files["file" ["Error"]: An error code is displayed if there is an error uploading the file

      • UPLOAD_ERR_OK value: 0; No error occurred and the file upload was successful.
      • upload_err_ini_size value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
      • upload_err_form_size value: 2; The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form. (We can specify the maximum number of bytes in the form form for input type= ' hidden ' name= ' max_file_size ' value= ' attachments ')
      • upload_err_partial value: 3; Only part of the file is uploaded.
      • upload_err_no_file value: 4; No files were uploaded.
Ii. Common file types
    • xls: application/vnd.ms-excel
    • xlsx: Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    • ppt: application/vnd.ms-powerpoint
    • pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
    • Doc: Application/msword
    • docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
    • Zip: application/x-zip-compressed
    • rar: application/x-zip-compressed
    • wmv: video/x-ms-wmv
    • mp3: audio/mpeg
    • mp4: Video/mp4
    • gif: image/git
    • JPEG: Image/jpeg or Image/pjpeg
      Note: For IE, the type of the recognized JPG file must be pjpeg, and for FireFox, it must be JPEG.
Third, the file upload process
    • After the file is uploaded, it is stored in the temp directory by default, and it must be removed from the temp directory or moved elsewhere, and if not, it will be deleted. That is, regardless of whether or not the upload succeeds, the files in the temp directory will be deleted after the script executes. So before you delete it, copy it or move it to another location using PHP's copy () or move_upload_file () function, and it's time to upload the file.

    • When uploading a file with a form, be sure to add the attribute content enctype= "Multipart/form-data", otherwise you will be reported an exception when obtaining the file information with $_files[filename].

Iv. saving uploaded files (move_uploaded_file)

BOOL move_uploaded_file (string $filename, string $destination)

Parameters:

    • filename: The file name of the uploaded file.

    • destination: Move files to this location

return value:

    • Returns TRUE upon success.

    • If filename is not a valid upload file, nothing happens and Move_uploaded_file () returns FALSE.

    • If filename is a legitimate upload file, but for some reason it cannot be moved, nothing happens and Move_uploaded_file () returns FALSE. A warning is also issued.

How far simple example of uploading a file

Note the optional value for the enctype property in:<form> is ↓↓↓↓

value Description
application/x-www-form-urlencoded Encode all characters before sending (default)
Multipart/form-data Character encoding is not. You must use this value when you use a form that contains a file upload control.
Text/plain Spaces are converted to "+" plus signs, but special characters are not encoded.

1, upload the file form 1.html

<html><head><title>Uploading files</title></head><body><form Action=' 1.php ' method=' post ' enctype=' Multipart/form-data '>Select File:<input type=' file ' name=' imgfile ' /><br /><input type=' submit ' name= ' Submit ' value=' Submit ' /></form></body></html>

2. Script for processing files 1.php

<?php if($_files[' Imgfile '][' ERROR ']==0){#限制上传的文件类型和大小    if((($_files[' Imgfile '][' type ']=="Image/gif")|| ($_files[' Imgfile '][' type ']=="Image/jpeg")|| ($_files[' Imgfile '][' type ']=="Image/pjpeg")) && ($_files[' Imgfile '][' Size ']) >=30720){#注意: Folder upload must already exist, otherwise it will error!         if(File_exists ("upload/".$_files[' Imgfile '][' name ']))Echo "The file already exists, please do not repeat the upload. ";ElseMove_uploaded_file ($_files[' Imgfile '][' Tmp_name '],"upload/".$_files[' Imgfile '][' name ']);Echo "File upload is successful!" "; }Else{Echo "is not a JPEG or GIF picture format, or the file is less than 30k! please re-upload. "; }}?>
Vi. Acknowledgements
    • PHP file upload;
    • php:move_uploaded_file-manual;
    • The use of $_files variables in PHP;
    • PHP files, through the PHP $_files array easy to upload files;
    • PHP uploads common file types and $_files["file" ["type"].

Copyright NOTICE: This article for Lshare original article, need to reprint please contact me, have questions welcome comments or private messages.

Uploading of PHP files

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.