Using PHP to implement file upload _php tutorial

Source: Internet
Author: User
One of the frequently seen issues on the WebDev site is about file uploads. In this article I will explain how to implement the file in PHP
Upload. Design Upload Form

Our main goal is to complete the file upload from the local computer to the server. To do this, we need to make a table
Allows the user to select a file and submit it. Here is an example:



<title>File Upload Form</title>








Action=submit.php3 method= "POST" >
Select Upload File
Type= "File" >

Type= "Submit" >




Note the enctype= "Multipart/form-data" section in the table. This must not be wrong, or the server will not know you on the
Transfer files.

Design Upload Program

Now that we have finished the foreground part, let's consider carefully how the background receives the file and save it to our designated target
Tape it down. The following is the beginning of the use of 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 if a file has been uploaded to the server via if ($MyFile! = "None");
2. Copy the file to the specified location.
3. Delete the temporary file.

When you press the Submit button, the file will be uploaded to the server's temp directory from your computer. Files in the Temp directory
Name is a temporary file. You should use the name value of the file field to access it, here is $myfile. The real file name uses the file
The name value of the field plus "_name" to access it, here is $myfile_name. Use the copy () function to copy the temporary file $myfile
Under the specified directory, the copied file name is $myfile_name. Do not forget to delete temporary files after completion, otherwise you will have a lot of you
Files that you do not want.

Set file name

One thing that might make a programmer sleepless is trying to change the value of the property of the file field. Not many people know it
is impossible. Despite the fact that it is possible, the value of the Vaue property is not allowed to be set, like IE and Netscape. Sounds like a little bit.
Laugh, why can't I set an initial value, which makes it easier for users to use it? If you do that, you'll find that you're bringing
A security breach has come. Imagine that you are logged into my site and I can change the value of the file field in a table.
So is there a promise to stop me from uploading your/etc/passwd file? Further, I do not need you to press the Submit button, I can first set
Set the value of the file field and then simulate the commit action through a JavaScript program ... Wow ... I can handle anything on your machine.
The file. For this reason, the browser simply puts The value field of the file field in the tag is ignored.

Limit file Size

Another cool feature is the option to limit the size of the upload file. Just add a The tag is ready:



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 adding the "_size" variable to the Name property value of the FileName field. In our case,
is to use $myfile_size. So, if you want to tell the user the size of the uploaded file, you can do it as follows:

echo "You have just uploaded $MyFile _name";
echo "The size of the file is $MyFile _size";

Permissions

It is clear that you need permission to write to the target directory. If a user uploads a file with an anonymous text, his or her user name should be
"Bobody". This user must have permission to write to the target directory or 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

http://www.bkjia.com/PHPjc/314015.html www.bkjia.com true http://www.bkjia.com/PHPjc/314015.html techarticle one of the frequently seen issues on the WebDev site is about file uploads. In this article I will explain how to use PHP to implement file upload. Design Upload Form Our main goal is to finish ...

  • Related Article

    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.