Php document uploading

Source: Internet
Author: User
The ini_get (str) function is used to obtain Apache server parameters for php file uploading. for example, the maximum value of the uploaded file is echoini_get ("upload_max_filesize "); PHP file upload parameters: the default size of PHP file upload is 2 MB. to upload files that exceed this setting, you need to adjust some parameters such as PHP and apache to upload files in php.
Obtain Apache server parameters

Use the ini_get (str) function
For example, obtain the maximum echo ini_get ("upload_max_filesize") of the uploaded file ");

Parameters involved in file upload in PHP

PHP's default upload limit is 2 MB. to upload files that exceed this setting, you need to adjust some parameters such as PHP and apache. next, we will briefly introduce some parameters involved in php file upload:

  • File_uploads

Whether to allow file upload over HTTP. ON is enabled by default.

  • Upload_tmp_dir

Upload_tmp_dir indicates the temporary directory where PHP files are uploaded. to upload files, make sure the server does not have the permission to close the temporary files and write the files to the folder. if not specified, PHP uses the system default value.

  • Upload_max_filesize

Maximum file size that can be uploaded. the default value is 2 MB.

  • Post_max_size

Controls the maximum data size that PHP can receive in a form submission using the POST method. If you want to use the php file upload function, you need to change this value to a value greater than upload_max_filesize .......?????????? More ......

?

  • Max_input_time

The time for receiving data through POST, GET, and PUT is limited in seconds. If the running environment of the application is on the low-speed link, you need to add this value to adapt to the more time required to receive data.

  • Memory_limit

To avoid using a large amount of available memory by running scripts, PHP allows you to define the memory usage limit. Use the memory_limit variable to specify the maximum memory capacity variable memory_limit that can be used by a single script program. The value of memory_limit should be greater than the value of post_max_size.

  • Max_execution_time

Max_execution_time sets the time for PHP to wait for the script to be executed before the script is forcibly terminated. The time is calculated in seconds. This variable is useful when the script enters an infinite loop state. However, when there is a legal activity that takes a long time to complete (such as uploading large files), this function will also cause operation failure. In this case, you must consider increasing the value of this variable to prevent PHP from closing the script when the script is executing an important process.

  • For linux hosts, the php. conf file may exist under/etc/httpd/conf. d/access. conf/. This file may solve the file size restrictions of some systems.
Principles and implementation of PHP file Upload

Upload using php file functions

This code is divided into two files, one is upload.html and the other is upload. php.

  • Upload.html

Where,

  1. Note: 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.
  2. 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.
  3. 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, the maximum size of uploaded files in PHP settings will not expire. 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.
  • Upload. php
$ F = & $ HTTP_POST_FILES ['myfile']; $ dest_dir = 'uploads'; // sets the upload directory $ dest = $ dest_dir. '/'. date ("ymd "). "_". $ f ['name']; // Set the file name to the date plus the file name to avoid duplication. $ r = move_uploaded_file ($ f ['tmp _ name'], $ dest ); chmod ($ dest, 0755); // sets the attributes of the uploaded file
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 related to the file Upload
  1. Value: 0. If no error occurs, the file is uploaded successfully.
  2. Value: 1; the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
  3. Value: 2; the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
  4. Value: 3; only part of the file is uploaded.
  5. 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.