About PHP uploading data

Source: Internet
Author: User
Tags php file upload
About PHP Uploading files

Get Apache Server Parameters

Using the Ini_get (str) function
For example, get the maximum value of the uploaded file Echo ini_get ("upload_max_filesize");

Parameters involved in uploading files to PHP

PHP default upload limit is maximum 2M, want to upload more than this set of files, need to adjust PHP, Apache and other parameters. Below, we briefly describe some of the parameters involved in uploading PHP files:

    • File_uploads

Whether to allow the switch to upload files over HTTP, the default is on

    • Upload_tmp_dir

Upload_tmp_dir used to explain the PHP uploaded files placed in the temporary directory, to upload files, you have to ensure that the server does not close the temporary files and write permissions to the folder, if not specified, PHP uses the system default value

    • Upload_max_filesize

Maximum allowable upload file size, default is 2M

    • Post_max_size

Controls the maximum amount of data that PHP can receive in a single form submission using the Post method. If you want to use PHP file upload functionality, you need to change this value to be larger than upload_max_filesize ...?????????? More......

?

    • Max_input_time

The time to receive data by post, get, and put is limited in seconds. If your application is running on a low-speed link, you need to increase this value to accommodate the additional time it takes to receive the data

    • Memory_limit

To prevent running scripts from using the system's available memory heavily, PHP allows you to define memory usage limits. Use the Memory_limit variable to specify a maximum memory capacity variable that can be used by a single script memory_limit value should be appropriate greater than post_max_size value

    • Max_execution_time

Max_execution_time sets the time at which PHP waits for the script to complete before forcing the script to terminate, which is calculated in seconds. This variable is useful when the script enters an infinite loop state. However, this feature also causes the operation to fail when there is a legitimate activity that takes a long time to complete, such as uploading large files. In such cases, you must consider increasing the value of this variable to avoid PHP closing the script when the script is performing some important process

    • For Linux hosts, there may be php.conf files under/etc/httpd/conf.d/access.conf/, which may solve some system file size limitations.

The principle and implementation of PHP file upload

Using PHP's file functions to implement uploads

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

    • Upload.html

which

    1. Please note This is a label, we want to implement the file upload, must be specified as Multipart/form-data, otherwise the server will not know what to do.
    2. It is worth noting that the hidden domain value of the form option max_file_size in file upload.html can limit the size of uploaded files by setting their value.
    3. The value of max_file_size is just a suggestion to the browser, in fact it can be simply bypassed. Therefore, do not put restrictions on the browser to hope for this value. In fact, the maximum number of uploaded files in PHP settings is not invalidated. But it's best to add max_file_size to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user finds it too big.
    • upload.php
$f =& $HTTP _post_files[' Myfile '; $dest _dir= ' uploads ';//Set upload directory $dest= $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//set file name as date plus filename to avoid duplicate $r=move_uploaded_file ($f [' Tmp_name '], $dest); chmod ($dest, 0755);//Set the properties of the uploaded file
Or

 
  

The contents of the $_files array in the example above are shown below. We assume that the name of the File upload field is UserFile (name can be arbitrarily named)

    • $_files[' UserFile ' [' Name '] The original name of the client machine file.
    • The MIME type of the $_files[' userfile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif".
    • $_files[' userfile ' [' size '] the size of the uploaded file, in bytes.
    • $_files[' UserFile '] [' tmp_name '] files are uploaded after the temporary file name stored on the server.
    • $_files[' userfile ' [' Error '] and the file upload related error code
    1. Value: 0; No error occurred and the file upload was successful.
    2. Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit 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 files were uploaded.
  • Related Article

    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.