One problem that is often seen on the WebDev site is about file uploads. In this article I will explain how to use PHP to implement file uploads.
Design Upload Form
Our main goal is to complete the file from the local computer upload to the server up. To do this, we need to make a table that allows the user to select a file and can submit it. Here is an example:
<HTML>
<HEAD>
<TITLE> File Upload Form </TITLE>
</HEAD>
<BODY>
<TABLE>
<form enctype= "Multipart/form-data" Name=myform action=submit.php3 method= "POST" >
<TR><TD> Select Upload file </td><td><input name= "MyFile" type= "file" ></TD></TR>
<TR><TD colspan= "2" ><input name= "submit" value= "upload" type= "submit" ></TD></TR>
</TABLE>
</BODY>
</HTML>
Note the enctype= "Multipart/form-data" section in the table. This must not be wrong, otherwise the server will not know you are uploading files.
Design Upload Program
Now that we've finished the front section, let's take a closer look at how the background is going to receive the file and save it to our designated directory. Here we start with PHP. This is the SUBMIT.PHP3 program:
?
If ($MyFile!= "None") {
Copy ($MyFile, "/home/berber/$MyFile _name");
Unlink ($MyFile);
}
else {
echo "You didn't upload any text?;
}
?>
Believe it or not, this is the whole process. What we do in the program is:
1. Check whether a file has been uploaded to the server, through if ($MyFile!= "none");
2. Copy the files to the specified location.
3. Delete temporary files.
When you press the Submit button, the file will be uploaded to the server's temporary directory from your computer. The file name in the temporary directory is a temporary file. You should use the name value of the file field to access it, here for $myfile. The real file name uses the name value of the file field plus "_name" to access it, here for $myfile_name. Use the copy () function to copy the temporary file $myfile to the specified directory, with the file named $myfile_name. Don't forget to delete the temporary files when you're done, or you'll have a lot of files you don't want.
Set file name
One thing that might keep a programmer awake is trying to change the value of the file field. Not many people know it is impossible. Although the consortium says it's OK, in fact, like IE and Netscape, it is not allowed to set the value of the Vaue property. It sounds a little funny, why can't I set an initial value that makes it easier for users to use? If you do that, you will find that you have brought a security loophole. You can imagine that you log on to my site and I can change the value of the file field in a table. So there's a promise to stop me from uploading your/etc/passwd file? Further, I do not need you to press the Submit button, I can set the value of the file field first, and then through a JavaScript program to simulate the submission action ... Wow .... I can handle any file on your machine. For this reason, the browser simply ignores the value field of the File field in the <INPUT> tag.
Limit file Size
Another cool feature is the option to limit the size of uploaded files. Just add a <INPUT> tag to it:
<input type= "hidden" name= "max_file_size" value= "100000" >
This will not allow users to upload more than 100KB of files.
Show file Size
In order to display the file size, you can access it by using the File Field Name property value plus the variable "_size". In our case, we use $myfile_size. So, if you want to tell the user the size of the uploaded file, you can do it like the following:
echo "You have just uploaded $MyFile _name";
echo "The size of the file is $MyFile _size";
Permissions
Obviously you need permission to write to the target directory. If a user uploads a file with a stealth text, his username should be
"Bobody". This user must have permission to write to the target directory otherwise you may get a message like the following:
Warning:unable to create '/home/berber/berber.txt ':
Permission denied
IN/HOME/BERBER/SUBMIT.PHP3 on line 5