If you want to know the detailed steps for uploading a file, I will take you through the detailed analysis below. With PHP, you can always complete a specific task in multiple ways. Let's take file upload as an example. Of course, you can use the traditional method to upload HTTP files and directly transfer the files to the Web server disk. You can also upload files in a more exotic way, using the FTP protocol in two steps: from your local hard disk to the Web server, and then to the FTP server.
PHP supports both FTP and HTTP uploads on the local machine, so you can make the best choice according to your application design needs. Using the FTP function of PHP for file transfer is almost the same as using the traditional FTP client-you will see that the name of the function is similar to the standard FTP command. There are already many articles about HTTP File Upload, which is why it is necessary to focus on FTP-based file upload (but in the example below, ). Note that this tutorial assumes that you have installed PHP/Apache and the HTTP File Upload and FTP functions are activated. This article can be downloaded from here. All source code is listed in a separate text format.
Step 1: Make sure you have the permission to connect to/upload to the FTP server
Php ftp functions require client-server connection, so you need to log on to the target server before uploading files. Your first task is to make sure that you have the trust book to complete this task. This step may seem to be taken for granted, but you will be surprised to find how many developers have forgotten to do so. As a result, they will waste a lot of time to solve the problem. You can log on to the target server by using the FTP client of the command line to check the connection status and try to upload a fake file.
PHP File Upload Code List
- $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 confirm that you have the relevant access permissions, you can log in.
Step 2: Create an upload form
Then, create a simple HTML form and ask the user about the access information of the FTP server, the directory uploaded by the server, the complete directory and the name of the uploaded file. The following example shows the form style.
PHP file upload code list B
- <html>
- <head></head>
- <body>
- <h2>Pleaseprovidethefollowinginformation:</h2>
-
- <formenctypeformenctype="multipart/form-data"method="post"action="upload.php">
- <inputtypeinputtype="hidden"name="MAX_FILE_SIZE"value="5000000"/>
- Host<br/>
- <inputtypeinputtype="text"name="host"/><p/>
- Username<br/>
- <inputtypeinputtype="text"name="user"/><p/>
- Password<br/>
- <inputtypeinputtype="password"name="pass"/><p/>
- Destinationdirectory<br/>
- <inputtypeinputtype="text"name="dir"/><p/>
- File<br/>
- <inputtypeinputtype="file"name="file"/><p/>
- <inputtypeinputtype="submit"name="submit"value="UploadFile"/>
- </form>
- </body>
- </html>
Here, the <inputtype = file...> element is used to generate the file selection dialog box. You can use it to select the file to be uploaded. <Formenctype =...> the Element then encodes the form data into multiple parts for submission, which makes it easier for PHP to identify submitted file components.
Step 3: Create a PHP Upload Handler
Once the form is submitted to the Web server, the next step (and the last step) is to use the FTP function of PHP to transmit it to the target server according to the user-provided access trust book. The following is the script (upload. php) That completes all the above work ).
PHP file upload 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");
- ?>