Detailed explanation of PHP file upload_PHP tutorial

WBOY
Release: 2016-07-13 17:15:51
Original
806 people have browsed it

File upload is divided into two parts, the HTML display part and the PHP processing part. The HTML part is mainly used to allow users to select the files to be uploaded, and then receive them in the background through $_FILES in PHP, and then upload the files to in the specified directory.

HTML part:

The code is as follows Copy code
 代码如下 复制代码

上传:


Upload:

 代码如下 复制代码


if($_FILES['myfile']['name'] != '') {

if($_FILES['myfile']['error'] > 0) {

echo "错误状态:" . $_FILES['myfile']['error'];

} else {

move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/" .

$FILES['myfile']['name']);

echo "<script>alert(上传成功!);</script>";

}

} else{

echo "<script>alert(请上传文件!);</script>";

}

?>

Description: The action="upload.php" in the form tag means that when you click submit in this form, the upload command will be sent to the page called upload.php for processing. method="post" refers to sending in post mode. The enctype="multipart/form-data" attribute specifies which content type to use when submitting this form. When the form requires binary data, such as file content, please use "multipart/form-data", this attribute is necessary if you want to upload files. Type="file" in input specifies that the input should be processed as a file, and there will be a browse button behind the input. PHP part:
The code is as follows Copy code
<🎜>if($_FILES['myfile']['name'] != '') {<🎜> <🎜>if($_FILES['myfile']['error'] > 0) { echo "Error status:" . $_FILES['myfile']['error']; } else { move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/" . $FILES['myfile']['name']); echo "<script>alert(Upload successful!);</script>"; } } else{ echo "<script>alert(Please upload the file!);</script>"; } ?>


Description:

Before explaining this code, we need to understand the following knowledge.

$_FILES['myfile']['name'] refers to the name of the uploaded file

$_FILES['myfile']['type'] refers to the type of file being uploaded

$_FILES['myfile']['size'] refers to the size of the uploaded file, in bytes (B)

$_FILES['myfile']['tmp_name'] refers to the name of the temporary copy file of the uploaded file stored in the server. After the file is moved to the specified directory, the temporary file will be automatically destroyed.

$_FILES['myfile']["error"] refers to the status code of errors that may occur during file upload. The meaning of each status will be explained later.

First of all, myfile in $_FILES['myfile']['name'] refers to the name value of the file tag uploaded in the above HTML page. Based on this, we can know which input submitted the file we are processing. , and then check whether $_FILES['myfile']['name'] is empty. Based on this, we can know whether the user has uploaded files and perform different operations. If the file is uploaded and the status is 0, it means the upload is successful. We can use the move_uploaded_file method to store the uploaded file in the specified directory. The above example refers to moving the uploaded file to the uploads folder in the same directory. This path It is a relative directory relative to this PHP file (upload.php). For example, if we want to move the uploaded file to a folder called user one level above upload.php, we can write like this: move_uploaded_file($_FILES['myfile']['tmp_name'], "../user /" . $FILES['myfile']['name']), this method is very convenient and flexible to use. In this way, a file is uploaded to the server, and the directory in the server can be opened to view the file. Allowing users to upload files is an act that carries huge security risks. Therefore, usually, we will impose some restrictions on the files uploaded by users, as follows:

The code is as follows Copy code
 代码如下 复制代码

if($_FILES['myfile']['name'] != '') {

if($_FILES['myfile']['error'] > 0) {

echo "错误状态:" . $_FILES['myfile']['error'];

} else {

if($_FILES['myfile']['type'] == 'image/jpeg' or $_FILES['myfile']['type'] ==

'image/pjpeg' or $_FILES['myfile']['type'] == 'image/gif' &&

$_FILES['myfile']['size'] < 20480){

move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/" .

$FILES['myfile']['name']);

echo "<script>alert(上传成功!);</script>";

} else {

echo "

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!