Three-step FTP implementation of PHP upload file code anatomy _php Tutorial

Source: Internet
Author: User
Want to know the detailed steps to upload the file, below I will take you to detailed analysis of it. With PHP, you can always have a number of ways to accomplish a particular task. Let's take a file upload for example. Of course, you can use HTTP file uploads in the traditional way, transferring files directly to the Web server disk. You can also upload in a more exotic way, upload it in two steps with the FTP protocol: from your local hard drive to the Web server, and then to the FTP server.

PHP supports both FTP and HTTP uploads natively, so you can make the best choice based on your application's design needs. Using PHP's FTP functions for file transfer is almost identical to using a traditional FTP client-you'll see that the name of the hyphen is similar to the standard FTP command. There are many more articles about HTTP file uploads, which is why this article needs to focus on FTP-based file uploads (but in the examples given later, you will see them). Note that this tutorial assumes that you have installed the Php/apache, and that the HTTP file upload and FTP functions have been activated. This article can be downloaded from here, and all the source code is listed in separate text form.

The first step: Make sure you have permission to connect/upload to the FTP server

PHP's FTP function requires a client-server connection, so you need to log on to the target server before uploading the file. Your first task is to make sure that you already have a letter of trust to complete the task. This step may seem natural, but you'll be amazed at how many developers forget to do so, and then waste a lot of time solving the problems that arise. You can log on to the target server by using the command line FTP client to check the connection status and attempt to upload a fake file.

PHP Upload file code List A

 
 
  1. $ftp
  2. FTP > opensome.host.com
  3. Connectedtosome.host.com.
  4. 220welcometoleonftpserver!
  5. User:upload
  6. 331useruploadokay,needpassword.
  7. password:***
  8. 230Restricteduserloggedin.
  9. FTP > bin
  10. 200Typeokay.
  11. FTP > Hash
  12. Hashmarkprintingon?ftp: (2048bytes/hashmark).
  13. FTP > Putfile.bin
  14. 200PORTcommandsuccessful.
  15. 150OpeningBINARYmodedataconnection.
  16. ##
  17. 226Transfercompleted.
  18. Ftp:4289bytessentin0.00seconds4289000.00kbytes/sec.
  19. FTP > Bye
  20. 221Goodbye.

Once you have identified the relevant access rights, you can log in.

Step Two: Create an upload form

Then, create a simple HTML form to ask the user important parameters--ftp the server's access information, the server's uploaded directory, and the full directory and the name of the uploaded file. The following example is the style of this form.

PHP Upload file code list b

 
 
  1. <html>
  2. <head> head>
  3. <body>
  4. < H2 > pleaseprovidethefollowinginformation: h2>
  5. < Formenctype Formenctype = "Multipart/form-data" Method = "POST" Action = "upload.php" >
  6. < InputType InputType = "hidden" name = "Max_file_size" value = "5000000" />
  7. Host <br/>
  8. < InputType InputType = "text" name = "Host" /><p/>
  9. Username <br/>
  10. < InputType InputType = "text" name = "User" /><p/>
  11. Password <br/>
  12. < InputType InputType = "Password" name = "Pass" /><p/>
  13. destinationdirectory <br/>
  14. < InputType InputType = "text" name = "dir" /><p/>
  15. File <br/>
  16. < InputType InputType = "File" name = "File" /><p/>
  17. < InputType InputType = "Submit" name = "Submit" value = "UploadFile" />
  18. form>
  19. body>
  20. html>

Here, the element is used to generate a file selection dialog where the user selects the file to be uploaded. element then encodes the form data into multiple partial commits, which makes it easier for PHP to identify the submitted file component.

Step three: Create a PHP upload handler

Once the form is submitted to the Web server, the next (and final) step is to use PHP's FTP function to transfer it to the target server in accordance with the user-supplied access trust book. Here is the script (upload.php) that completes all of the above work.

PHP Upload file code list C

 
 
  1. PHP
  2. Getftpaccessparameters
  3. $ Host =$_post[' host '];
  4. $ User =$_post[' user '];$ Pass =$_post[' pass ';
  5. $ DestDir =$_post[' dir '];
  6. $ Workdir = "/usr/local/temp" ;//definethisasperlocalsystem
  7. Gettemporaryfilenamefortheuploadedfile
  8. $ Tmpname = basename ($_files[' file ' [' Tmp_name ']);
  9. Copyuploadedfileintocurrentdirectory
  10. Move_uploaded_file ($_files[' file '] [' tmp_name '], $workDir. " /". $tmpName) Ordie (" Cannotmoveuploadedfiletoworkingdirectory ");
  11. OpenConnection
  12. $ Conn = Ftp_connect ($host) Ordie ("Cannotinitiateconnectiontohost");
  13. Sendaccessparameters
  14. Ftp_login ($conn, $user, $pass) Ordie ("Cannotlogin");
  15. Performfileupload
  16. $ Upload = Ftp_put ($conn, $destDir. " /". $_files[' file ' [' name '], $workDir." /". $tmpName, Ftp_binary);
  17. Checkuploadstatus
  18. DisplayMessage
  19. if (! $upload) {
  20. echo "Cannotupload";
  21. }else{
  22. echo "Uploadcomplete";
  23. }
  24. Closetheftpstream
  25. Ftp_close ($conn);
  26. Deletelocalcopyofuploadedfile
  27. Unlink ($workDir. " /". $tmpName) Ordie (" cannotdeleteuploadedfilefromworkingdirectory--manualdeletionrecommended ");
  28. ?>

http://www.bkjia.com/PHPjc/446550.html www.bkjia.com true http://www.bkjia.com/PHPjc/446550.html techarticle want to know the detailed steps to upload the file, below I will take you to detailed analysis of it. With PHP, you can always have a number of ways to accomplish a particular task. We'll take the 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.