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:
Whether to allow the switch to upload files over HTTP, the default is on
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
Maximum allowable upload file size, default is 2M
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......
?
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
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 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
which
- 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.
- 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.
- 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.
$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
- Value: 0; No error occurred and the file upload was successful.
- Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit 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 files were uploaded.