PHP, File upload _php tutorial

Source: Internet
Author: User
In PHP, file uploads are generally implemented through Move_uploaded_file ().
BOOL Move_uploaded_file (string filename, string destination) This function checks and ensures that the file specified by filename is a valid upload file (that is, the HTTP POST upload mechanism via PHP Transmission). If the file is valid, move it to the file specified by destination.

If filename is not a valid upload file, nothing happens and Move_uploaded_file () returns FALSE.

If filename is a legitimate upload file, but for some reason it cannot be moved, nothing happens and Move_uploaded_file () returns FALSE. A warning is also issued.

Here's an example of how PHP implements file uploads.
Test.php's original file
Copy CodeThe code is as follows:


<title>Site File Upload Instance</title>






Max_file_size hidden Fields (in bytes) must precede the file input field with a value of the maximum size of the received file. This is a suggestion for the browser, PHP will also check this. This setting can be bypassed easily on the browser side, so do not expect this feature to block large files. In fact, the maximum upload file value in PHP settings is not invalidated. However, it is best to add this item to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too large to upload.

Note: To ensure that the properties of the file upload form are enctype= "Multipart/form-data", the file will not be uploaded.

In processing the upload page, the information for uploading the file is obtained through the array $_files. We assume that the name of the file upload field, as shown in the example above, is "Upfile" and the file information is:

$_files[' upfile ' [' Name ']

The original filename of the uploaded file.

$_files[' upfile ' [' type ']

The MIME type of the file, if the browser provides this information. An example is "Image/gif". However, this MIME type is not checked on the PHP side, so do not assume this value is assumed.

$_files[' upfile ' [' Size ']

The size of the uploaded file, in bytes.

$_files[' upfile ' [' Tmp_name ']

Temporary file name stored on the server after uploading files.

$_files[' upfile ' [' Error ']

The error code associated with the file upload.

PHP will return a corresponding error code along with the file information array. The code can be found in the Error field in the file array that is generated when the file is uploaded. The code is as follows:

Upload_err_ok

The value is 0 and the file upload is successful.

Upload_err_ini_size

The value is 1, the uploaded file exceeds the upload_max_filesize option limit in php.ini, and the default value is 2M. If you want to upload a larger, we can find in php.ini upload_max_filesize = 2M modified to take effect.

Upload_err_form_size

Its value is 2, and 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 upload is not complete, only partially uploaded.

Upload_err_no_file

The value is 4 and the file upload fails.

Upload_err_no_tmp_dir

The value is 6 and the Temp folder cannot be found. PHP 4.3.10 and PHP 5.0.3 introduced.

Upload_err_cant_write

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

Note: The above values become PHP constants after PHP 4.3.0.
Action.php's original code
/**
Welcome to our station to Exchange learning experience

*/

function GetName ($exname) {
$dir = "tmp/";
$i = 1;
if (!is_dir ($dir)) {
mkdir ($dir, 0777);
}

while (true) {
if (!is_file ($dir. $i. ".". $exname)) {
$name = $i. ".". $exname;
Break
}
$i + +;
}

return $dir. $name;
}

$exname =strtolower (substr ($_files[' upfile ' [' Name '], (Strrpos ($_files[' upfile ' [' Name '], '. ') +1)));
$uploadfile = GetName ($exname);

if (Move_uploaded_file ($_files[' upfile ' [' tmp_name '], $uploadfile)) {
echo "

File Upload Successful!



";
}else {
echo "

File upload failed!



";
}
echo "Here are some information about file uploads:


Original filename: ". $_files[' upfile ' [' Name '].
"

Type: ". $_files[' upfile ' [' type '].
"

Temporary file name: ". $_files[' upfile ' [' tmp_name '].
"

File size: ". $_files[' upfile ' [' Size '].
"

Error code: ". $_files[' upfile ' [' Error '];
?>

Be sure to ensure that the file folder after uploading the permissions to 777, which is particularly evident on the server, sometimes we write the upload code without any errors, but on the internet can not achieve the upload function, the reason is here.

This article just talk about how to implement the file upload through PHP, have not seen this part of the friends can refer to.

http://www.bkjia.com/PHPjc/317265.html www.bkjia.com true http://www.bkjia.com/PHPjc/317265.html techarticle in PHP, file uploads are generally implemented through Move_uploaded_file (). Boolmove_uploaded_file (stringfilename,stringdestination) This function checks and ensures that the file specified by filename ...

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