Analysis of php file upload methods-php Tutorial

Source: Internet
Author: User
Analysis on methods for uploading files in php

Note:

To upload a file, you must specify it as multipart/form-data. In addition, the hidden Value range of the form option MAX_FILE_SIZE in the Upload file upload.html can be limited by setting its Value. The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. PHP sets the maximum number of uploaded files, which is not invalid. However, it is best to add MAX_FILE_SIZE to the form to improve the upload experience.

2. upload. php

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

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

$ _ FILES ['userfile'] ['name'] The original name of the client machine file. $ _ FILES ['userfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/gif ". $ _ FILES ['userfile'] ['size'] size of the uploaded file, in bytes. $ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded. $ _ FILES ['userfile'] ['error'] error code value related to the file upload: 0; no error occurs, and 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.