Simple and Fast PHP file Upload usage summary _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Summary of simple and quick PHP file upload methods. PHP has been developing for a long time and many users are familiar with PHP. here I will share my personal understanding and discuss it with you. the support for PHP file uploading is becoming more and more perfect, after a long history of PHP development in PHP4.0.3, many users know PHP very well. here I will share my personal understanding and discuss it with you. the support for PHP file uploading is becoming more and more perfect, before PHP 4.0.3, we can only use the copy () and is_uploaded_file () methods to upload files. In fact, PHP is much easier to upload files than ASP, this is a small proud place for me to choose PHP.

After PHP 4.0.3, PHP has a new function move_uploaded_file (). Uploading a file is a lot easier. you only need this function. (I am not saying that uploading a file system is enough to have only one function. do not misunderstand what I mean, I mean move_uploaded_file is a core function for uploading files ). Now, let's take a look at the three functions (from the PHP Manual ). Copy (string source, string desk); copy the file from source to dest. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

PHP file upload example:

 
  1. if (!copy($file, $file.’.bak’)) {
  2. print (”failed to copy $file…<br>n”);
  3. }
  4. is_uploaded_file(string filename)

If the file given by filename is uploaded through http post, TRUE is returned.

 
  1. move_uploaded_file(string filename, string destination)

This function checks and ensures that the file specified by filename is a valid PHP file to be uploaded (that is, the file is uploaded through the http post Upload mechanism of PHP ). If the file is valid, move it to the file specified by destination. The interpretation of the three functions can draw a conclusion that move_uploaded_file () is a combination of the copy () and is_uploaded_file () functions, after talking about these three functions in such a half day, they may be a little unfamiliar. here are two examples: first, create a front page and name it index.html.

 
  1. <form enctype=”multipart/form-data” action=”_URL_” method=”post”>
  2. Send this file: <input name=”filename” type=”file”>
  3. <input type=”submit” value=”Send File”>
  4. form>

1. The following describes how to upload files using the copy () and is_uploaded_file () methods (save as: copy.php, upload the file, and change _ URL _ in index.html to copy. php)

 
  1. Php
  2. $Path= "./Uploadfiles/"; // file Upload path
  3. If (is_uploaded_file ($ filename ))
  4. // Note that $ filename is passed to is_uploaded_file. do not pass $ _ FILES ["filename"] ["name"].
  5. {
  6. $File1= $ _ FILES ["filename"] ["name"];
  7. // Note that the $ _ POST [] method is supported by PHP4.1.0 and later. The $ HTTP_POST_FILES [] method is used for versions earlier than PHP.
  8. $File2= $ Path. time (). $ file1;
  9. $Flag=1;
  10. }
  11. If ($ flag) $Result=Copy($ File1, $ file2 );
  12. If ($ result) echo "Upload successful !";
  13. ?>

2. The following describes how to upload files using the move_uploaded_file () method (save as: move.php, upload the file, and change _ URL _ in index.html to move. php)

 
  1. Php
  2. $Path= "./Uploadfiles/"; // Upload path
  3. If ($ _ FILES ["filename"] ["name"])
  4. {
  5. $File1= $ _ FILES ["filename"] ["name"];
  6. $File2= $ Path. time (). $ file1;
  7. $Flag=1;
  8. }
  9. If ($ flag) $Result=Move_uploaded_file($ _ FILES ["filename"] ["tmp_name"], $ file2 );
  10. // Note that the first parameter passed to move_uploaded_file is the temporary file uploaded to the server.
  11. If ($ result) echo "Upload successful !";
  12. ?>

This is just two small programs. In fact, the code required to complete file upload is not the case. There are also file size restrictions, file extension selection, and some other security solutions. I wrote this article, and everyone is interested in writing a complete file upload system. Haha !!!


4.0.3...

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.