As is known to all, file uploads are an essential part of some website applications. such as personal blog upload personality Avatar, some forums to share good learning materials, which involves the use of the form to process file upload knowledge, in PHP we can use $_files this global array to handle. Here's a look at the basic File upload Processing section. First of all, to be able to upload a file must be a security risk, so in the development of this part of the function, we must pay attention to the security of the site. And this article is only the basic file upload part, about the security aspects of the less, I hope in the future of advanced applications can learn.
In the section on pre-defined variables in PHP we have some approximate learning about some of the predefined variables, in which we have $_files this global variable, and now we are going to learn more about this global volume, because I use it when I am working on file uploads.
$_files parameter Details:
$_files["File" ["Name"]– the name of the file being uploaded
$_files["File" ["type"]– the types of files being uploaded
$_files["File" ["Size"]– of the uploaded file, in bytes
$_files["File" ["Tmp_name"]– the name of the temporary copy of the file stored on the server
$_files["File" ["Error"]– error code caused by file upload
$_files[["Error"] value in "File" ["Error"]:
Upload_err_ok
0: No error occurred, file upload succeeded
Upload_err_ini_size
1: The uploaded file exceeds the value of the upload_max_filesize (default 2M) option limit in php.ini
Upload_err_form_size
2: The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form
Upload_err_partial
3: Files are only partially uploaded
Upload_err_no_file
4: No files are uploaded
5: Transfer file size to 0
Let's take a look at the most basic file uploads:
<body>
<form action= "upload-file.php" method= "POST"
Enctype= "Multipart/form-data" >
<label for= "File" > file name:</label>
<input type= "File" name= "file" id= "file"/>
<br/>
<input type= "Submit" name= "Submit" value= "Submission"/>
</form>
</body>
This HTML page is as follows:
The upload-file.php code is as follows:
<?php
if ($_files["file" ["error"] > 0)
{
echo "Error:". $_files["File" ["Error"]. "<br/>";
}
Else
{
echo "File name:". $_files["File" ["Name"]. "<br/>";
echo "Type:". $_files["File" ["type"]. "<br/>";
echo "Size:". ($_files["File" ["Size"]/1024). "Kb<br/>";
echo "Storage location:". $_files["File" ["Tmp_name"];
}
?>
We randomly upload a Word file to see how the results are handled:
File name: Css.doc
Type: Application/msword
Size: 81.5 Kb
Storage location: C:\WINDOWS\temp\php7D.tmp
As you can see, this file is saved to the C:\WINDOWS\temp\ temp directory, you may want to see if this file exists in this directory, but the result is: No! Why? Because PHP after the execution of this script, the resulting file is deleted, so after uploading further processing. Also casually mention, under Windows, PHP generated temporary files such as php7d.tmp is regular, that is, the next time through the form upload file should be like this: php7e.tmp
To save our uploaded files, use the Move_uploaded_file function. Create a upload folder in your upload-file.php file with the same directory. Let's take a look at the example above:
<?php
if ($_files["file" ["error"] > 0)
{
echo "Error:". $_files["File" ["Error"]. "<br/>";
}
Else
{
echo "File name:". $_files["File" ["Name"]. "<br/>";
echo "Type:". $_files["File" ["type"]. "<br/>";
echo "Size:". ($_files["File" ["Size"]/1024). "Kb<br/>";
}
if (file_exists ("upload/". $_files["File" ["Name"]))
{
echo $_files["File" ["Name"]. "The file already exists.";
}
Else
{
Move_uploaded_file ($_files["file"] ["Tmp_name"],
"Upload/". $_files["File" ["name"]);
echo "file has been stored in:". " Upload/". $_files["File" ["Name"];
}
?>
Let's take a look at the results:
File name: Css.doc
Type: Application/msword
Size: 81.5 Kb
File has been stored in: Upload/css.doc
After this step, we can get the file we uploaded in the place we specified (upload). Here, the most basic file upload is over. Also say, we upload the time must consider the type of file, to consider the size of files and so on. about these aspects with the study in depth to introduce it
PHP $_files processing file uploads