Case study of PHP File Upload using file functions

Source: Internet
Author: User
Tags php file upload

If you are a computer enthusiast and do not know PHP, you will be too outdated. If you want to know about PHP, let's take a look at the implementation of PHP file upload. This code is divided into two files, one is upload.html and the other is upload. php.

 
 
  1. <Form Enctype="Multipart/form-data" Action="Upload. php" Method="Post"> 
  2. <Input Type="Hidden" Name="Max_file_size" Value="100000"> 
  3. <Input Name="Userfile" Type="File">
  4. <Input Type="Submit" Value="Upload files"> 
  5. </Form> 

Note that <form enctype = "multipart/form-data" ......> this is a tag. to upload a file, you must specify it as multipart/form-data. Otherwise, the server will not know what to do. The Value is the hidden Value range of the form option MAX_FILE_SIZE in the file upload.html. You can set the Value to limit the size of the uploaded file. The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. Therefore, do not forward browser restrictions to this value. In fact, PHP sets the maximum number of PHP files to be uploaded. But it is best to add MAX_FILE_SIZE to the form, because it can avoid the trouble of finding the file too big after you wait for the upload of a large file.

 
 
  1. Upload. php
  2. $F= & $ HTTP_POST_FILES ['myfile'];
  3. $Dest_dir='Uploads'; // Set the upload directory
  4. $Dest= $ Dest_dir. '/'. date ("ymd"). "_". $ f ['name']; // set the file name to date and add the file name to avoid duplication.
  5. $R=Move_uploaded_file($ F ['tmp _ name'], $ dest );
  6. Chmod ($ dest, 0755); // sets the attributes of the uploaded file

Or

 
 
  1. <?copy($_FILES[MyFile][tmp_name],$_FILES[MyFile][name]);?> 

In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile)

 
 
  1. $ _ FILES ['userfile'] ['name'] // the original name of the client machine file.
  2. $ _ FILES ['userfile'] ['type'] // MIME type of the file, which must be supported by the browser, for example, "image/gif ".
  3. $ _ FILES ['userfile'] ['SIZE'] // size of the uploaded file, in bytes.
  4. $ _ FILES ['userfile'] ['tmp _ name'] // temporary file name stored on the server after the file is uploaded.
  5. $ _ FILES ['userfile'] ['error'] // error Code related to the File Upload

PHP File Upload value analysis:
◆ Value: 0. If no error occurs, the file is uploaded successfully.
◆ Value: 1; the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
◆ Value: 2; the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
◆ Value: 3; only part of the file is uploaded.
◆ Value: 4; no file is uploaded.


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.