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
- $ftp
- FTP > opensome.host.com
- Connectedtosome.host.com.
- 220welcometoleonftpserver!
- User:upload
- 331useruploadokay,needpassword.
- password:***
- 230Restricteduserloggedin.
- FTP > bin
- 200Typeokay.
- FTP > Hash
- Hashmarkprintingon?ftp: (2048bytes/hashmark).
- FTP > Putfile.bin
- 200PORTcommandsuccessful.
- 150OpeningBINARYmodedataconnection.
- ##
- 226Transfercompleted.
- Ftp:4289bytessentin0.00seconds4289000.00kbytes/sec.
- FTP > Bye
- 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
- <html>
- <head> head>
- <body>
- < H2 > pleaseprovidethefollowinginformation: h2>
- < Formenctype Formenctype = "Multipart/form-data" Method = "POST" Action = "upload.php" >
- < InputType InputType = "hidden" name = "Max_file_size" value = "5000000" />
- Host <br/>
- < InputType InputType = "text" name = "Host" /><p/>
- Username <br/>
- < InputType InputType = "text" name = "User" /><p/>
- Password <br/>
- < InputType InputType = "Password" name = "Pass" /><p/>
- destinationdirectory <br/>
- < InputType InputType = "text" name = "dir" /><p/>
- File <br/>
- < InputType InputType = "File" name = "File" /><p/>
- < InputType InputType = "Submit" name = "Submit" value = "UploadFile" />
- form>
- body>
- 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
- PHP
- Getftpaccessparameters
- $ Host =$_post[' host '];
- $ User =$_post[' user '];$ Pass =$_post[' pass ';
- $ DestDir =$_post[' dir '];
- $ Workdir = "/usr/local/temp" ;//definethisasperlocalsystem
- Gettemporaryfilenamefortheuploadedfile
- $ Tmpname = basename ($_files[' file ' [' Tmp_name ']);
- Copyuploadedfileintocurrentdirectory
- Move_uploaded_file ($_files[' file '] [' tmp_name '], $workDir. " /". $tmpName) Ordie (" Cannotmoveuploadedfiletoworkingdirectory ");
- OpenConnection
- $ Conn = Ftp_connect ($host) Ordie ("Cannotinitiateconnectiontohost");
- Sendaccessparameters
- Ftp_login ($conn, $user, $pass) Ordie ("Cannotlogin");
- Performfileupload
- $ Upload = Ftp_put ($conn, $destDir. " /". $_files[' file ' [' name '], $workDir." /". $tmpName, Ftp_binary);
- Checkuploadstatus
- DisplayMessage
- if (! $upload) {
- echo "Cannotupload";
- }else{
- echo "Uploadcomplete";
- }
- Closetheftpstream
- Ftp_close ($conn);
- Deletelocalcopyofuploadedfile
- Unlink ($workDir. " /". $tmpName) Ordie (" cannotdeleteuploadedfilefromworkingdirectory--manualdeletionrecommended ");
- ?>
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 ...