Home >Backend Development >PHP Tutorial >File upload in PHP_PHP tutorial

File upload in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:59:39749browse

In PHP, file upload is 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 legal uploaded file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by destination.

If filename is not a legal uploaded file, no operation will occur and move_uploaded_file() will return FALSE.​

If filename is a legal uploaded file but cannot be moved for some reason, no operation will occur and move_uploaded_file() will return FALSE. A warning is also issued.

Below we use an example to illustrate how PHP implements file upload.
The original file of test.php

Copy the code The code is as follows:



Website file upload example
-->





 
 
 
 

MAX_FILE_SIZE hidden field (unit: bytes) must be placed before the file input field, and its value is the maximum size of the received file. This is a recommendation for browsers, PHP will also check this. This setting can be easily bypassed on the browser side, so don't expect to use this feature to block large files. In fact, the maximum upload file size in PHP settings will not expire. But it is better to add this item to the form, because it can avoid the trouble of users spending time waiting for large files to be uploaded only to find that the file is too large and the upload fails.

Note: Make sure the attribute of the file upload form is enctype="multipart/form-data", otherwise the file will not be uploaded.

In the upload processing page, the uploaded file information is obtained through the array $_FILES. We assume that the name of the file upload field is "upfile" as shown in the above example, then the file information is:

$_FILES['upfile']['name']

Original text of the uploaded file File name.

$_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 don't take it for granted.

$_FILES['upfile']['size']

The size of the uploaded file, in bytes.

$_FILES['upfile']['tmp_name']

The temporary file name stored on the server after the file is uploaded.

$_FILES['upfile']['error']

Error code related to the file upload.

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

UPLOAD_ERR_OK

The value is 0, the file is uploaded successfully.

UPLOAD_ERR_INI_SIZE

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

UPLOAD_ERR_FORM_SIZE

The 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

The value is 3, the file upload is incomplete and only part of it is uploaded.

UPLOAD_ERR_NO_FILE

The value is 4 and the file upload failed.

UPLOAD_ERR_NO_TMP_DIR

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

UPLOAD_ERR_CANT_WRITE

The value is 7, file writing failed. PHP 5.1.0 introduced.

Note: The above values ​​became PHP constants after PHP 4.3.0.
The original code of action.php
/**
Welcome to our site to exchange learning experiences

*/

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 uploaded successfully!


< ;br>";
}else {
echo "

File upload failed!



";
}
echo "The following is some information about file upload:


Original file name: ".$_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 permissions of the folder where the file is uploaded is at least 777. This is on the server This is especially obvious in the above. Sometimes the upload code we write does not have any errors, but the upload function cannot be implemented on the Internet. This is the reason.

This article only talks about how to upload files through PHP. Friends who have not read this part can refer to it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317265.htmlTechArticleIn PHP, file upload is generally implemented through move_uploaded_file(). boolmove_uploaded_file(stringfilename,stringdestination) This function checks and ensures that the file specified by filename...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn