PHP Upload Files FAQ Summary, _php tutorial

Source: Internet
Author: User
Tags html form mime file

PHP Upload Files FAQ Summary,


The PHP upload files often encountered several problems summed up, later use will not have to look for.

1. Make a simple upload file first

Copy the Code code as follows:








Copy the Code code as follows:
<?php
if (($_files["File" ["Size"] < 20000)
{
if ($_files["file" ["error"] > 0)
{
echo "Return Code:". $_files["File" ["Error"]. "
";
}
Else
{
echo "Upload:". $_files["File" ["Name"]. "
";
echo "Type:". $_files["File" ["type"]. "
";
echo "Size:". ($_files["File" ["Size"]/1024). The Kb
";
echo "Temp file:". $_files["File" ["Tmp_name"]. "
";
if (file_exists ("upload/". $_files["File" ["Name"]))
{
echo $_files["File" ["Name"]. "already exists.";
}
Else
{
Move_uploaded_file ($_files["file"] ["Tmp_name"],
"Upload/". $_files["File" ["name"]);
echo "Stored in:". " Upload/". $_files["File" ["Name"];
}
}
}
Else
{
echo "Invalid file";
}
?>

2. Then understand the value of the Super global variable $_files

Copy the Code code as follows:
$_files[' UserFile ' [' Name ']
$_files[' userfile ' [' type ']
$_files[' userfile ' [' Size ']
$_files[' UserFile ' [' Tmp_name ']
$_files[' userfile ' [' Error ']

where all values of $_files[' userfile ' [' Error '] are:

Upload_err_ok The value is 0, no error occurred and the file upload was successful.

Upload_err_ini_size has a value of 1, the uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.

Upload_err_form_size its value is 2, the size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form.

Upload_err_partial its value is 3, the file is only partially uploaded.

Upload_err_no_file has a value of 4 and no files are uploaded.

Upload_err_no_tmp_dir its value is 6 and cannot find a temporary folder. PHP 4.3.10 and PHP 5.0.3 introduced.

Upload_err_cant_write The value is 7, the file write fails. PHP 5.1.0 introduced.

3. Many situations: need to strictly judge the upload file type

We know that using $_files[' userfile ' [' type '] is a very unwise way to determine which file type to upload, because the decision is based on the suffix of the file, and anyone can change the suffix of a mp3 file to JPG to disguise it as a picture. Therefore, PHP official recommended to use PHP extension php_fileinfo to determine the MIME file, open the way to expand Baidu a lot, win and Linux slightly different.

4. Scenario One: automatically rename after uploading files with duplicate name

Copy the Code code as follows:
if (File_exists ("./upload/". $_files["File" ["Name"]))
{
do{
$suffix = "";
$suffix _length = 4;
$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$len = strlen ($str)-1;
Append 4 random characters after file name
for ($i =0; $i < $suffix _length; $i + +) {
$suffix. = $str [rand (0, $len)];
}
$upload _filename = $_files[' file ' [' Name '];
$filename = substr ($upload _filename,0,strrpos ($upload _filename, ".")). $suffix. ".". substr ($upload _filename,strrpos ($_files["File" ["Name"], ".") +1);
}while (File_exists ("./upload/". $filename));
Move_uploaded_file ($_files["file"] ["Tmp_name"], "./upload/". $filename);
}else{
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]);
}

5. Scenario Two: upload files According to the date sub-catalogue

Copy the Code code as follows:
$structure = './'. Date ("Y"). ' /'. Date ("M"). ' /'. Date ("D"). ' /';
if (!mkdir ($structure, 0777, True)) {
Die (' Failed to create folders ... ');
}
Move_uploaded_file ($_files["file"] ["Tmp_name"], $structure. $_files["File" ["name"]);

6. Scenario Three: multiple file uploads

Copy the Code code as follows:

Copy the Code code as follows:
<?php
foreach ($_files["Pictures"] ["error"] as $key = $error) {
if ($error = = UPLOAD_ERR_OK) {
$tmp _name = $_files["Pictures" ["Tmp_name"] [$key];
$name = $_files["Pictures" ["Name"] [$key];
Move_uploaded_file ($tmp _name, "data/$name");
}
}
?>

In some cases, this variable structure of multiple files is not useful:

Copy the Code code as follows:
Array (1) {
["Upload"]=>array (2) {
["Name"]=>array (2) {
[0]=>string (9) "File0.txt"
[1]=>string (9) "File1.txt"
}
["Type"]=>array (2) {
[0]=>string (Ten) "Text/plain"
[1]=>string (Ten) "Text/html"
}
}
}

In many cases, we need a structure like this.

Copy the Code code as follows:
Array (1) {
["Upload"]=>array (2) {
[0]=>array (2) {
["Name"]=>string (9) "File0.txt"
["Type"]=>string (Ten) "Text/plain"
},
[1]=>array (2) {
["Name"]=>string (9) "File1.txt"
["Type"]=>string (Ten) "Text/html"
}
}
}

The structure can be easily converted using the following function

Copy the Code code as follows:
function Diverse_array ($vector) {
$result = Array ();
foreach ($vector as $key 1 = $value 1)
foreach ($value 1 as $key 2 = $value 2)
$result [$key 2][$key 1] = $value 2;
return $result;
}
$upload = Diverse_array ($_files["upload"]);

7. Sometimes: need to configure the server to modify the maximum upload file size

First, on the form


You can limit the upload file size (which can be bypassed).

And then you need to adjust the configuration on the server.

Ini:

Max_execution_time = 30 Maximum time per script run, per second
Max_input_time = 60, each script can consume the time, the unit is also the second
Memory_limit = 128M, this is the maximum memory consumed by the script run
Post_max_size = 8M, form submission maximum data is 8M, this item is not limited to upload a single file size, but for the entire form of submission data restrictions.
Upload_max_filesize = 2M, maximum license size for uploaded files

Nginx:

Copy the Code code as follows:
Location/{
root HTML;
Index index.html index.htm;
Client_max_body_size 1000m;
}

The above is the common problem of the treatment method, I hope you can like.

http://www.bkjia.com/PHPjc/953153.html www.bkjia.com true http://www.bkjia.com/PHPjc/953153.html techarticle php Upload files Frequently Asked questions summary, the PHP upload files often encountered several problems summed up, later use when not to look for. 1. First to make a copy of the simplest upload file ...

  • 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.