If you want to know the detailed steps of uploading files, I will take you to analyze them in detail below. With PHP, you always have multiple ways to accomplish a specific task. Let’s take file upload as an example. Of course, you can use HTTP file upload in the traditional way, transferring files directly to the web server disk. You can also upload in a more exotic way, using the FTP protocol to complete the upload in two steps: from your local hard drive to the web server, and then to the FTP server.
Step 1: Make sure you have the permission to connect/upload to the FTP server
PHP’s FTP function requires a client-server connection, so you need to process the file Log in to the target server before uploading. Your first task is to make sure you have the letters of trust to complete the task. This step may seem like a no-brainer, but you'd be surprised how many developers forget to do it, and end up wasting a lot of time solving problems that arise as a result. You can log into the target server using a command-line FTP client to check the connection and attempt to upload a fake file.
PHP upload file code list A
<ol class="dp-xml"> <li class="alt"><span><span>$ftp </span></span></li> <li class=""> <span>ftp</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>opensome.host.com </span> </li> <li class="alt"><span>Connectedtosome.host.com. </span></li> <li class=""><span>220WelcometoleonFTPServer! </span></li> <li class="alt"><span>User:upload </span></li> <li class=""><span>331Useruploadokay,needpassword. </span></li> <li class="alt"><span>Password:*** </span></li> <li class=""><span>230Restricteduserloggedin. </span></li> <li class="alt"> <span>ftp</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>bin </span> </li> <li class=""><span>200Typeokay. </span></li> <li class="alt"> <span>ftp</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>hash </span> </li> <li class=""><span>HashmarkprintingOn?ftp:(2048bytes/hashmark). </span></li> <li class="alt"> <span>ftp</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>putfile.bin </span> </li> <li class=""><span>200PORTcommandsuccessful. </span></li> <li class="alt"><span>150OpeningBINARYmodedataconnection. </span></li> <li class=""><span>## </span></li> <li class="alt"><span>226Transfercompleted. </span></li> <li class=""><span>ftp:4289bytessentin0.00Seconds4289000.00Kbytes/sec. </span></li> <li class="alt"> <span>ftp</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>bye </span> </li> <li class=""><span>221Goodbye. </span></li> </ol>
Once you confirm that you have the relevant access rights, you can log in.
Step 2: Create an upload form
Then, create a simple HTML form to ask the user for important parameters - FTP server access information, server upload directory, as well as the complete directory and the name of the uploaded file. The following example is the style of this form.
PHP upload file code list B
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>h2</SPAN><SPAN class=tag>></span></font></strong><span>Pleaseprovidethefollowinginformation:</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>h2</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formenctype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formenctype</FONT></STRONG></SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upload.php"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="hidden"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="MAX_FILE_SIZE"</SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"5000000"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class=""> <span>Host</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="text"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="host"</SPAN><STRONG><FONT color=#006699><SPAN class=tag>/></font></strong></span><span class="tag"><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></span><span> </span> </li> <li class=""> <span>Username</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="text"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="user"</SPAN><STRONG><FONT color=#006699><SPAN class=tag>/></font></strong></span><span class="tag"><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></span><span> </span> </li> <li class=""> <span>Password</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="password"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="pass"</SPAN><STRONG><FONT color=#006699><SPAN class=tag>/></font></strong></span><span class="tag"><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></span><span> </span> </li> <li class=""> <span>Destinationdirectory</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="text"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="dir"</SPAN><STRONG><FONT color=#006699><SPAN class=tag>/></font></strong></span><span class="tag"><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></span><span> </span> </li> <li class=""> <span>File</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="file"</SPAN><STRONG><FONT color=#006699><SPAN class=tag>/></font></strong></span><span class="tag"><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></span><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="submit"</SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"UploadFile"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>
Here, the
Step Three: Create the PHP Upload Handler
Once the form has been submitted to the web server, the next (and final) step is to use PHP’s FTP functions to follow The access credentials provided by the user are transferred to the target server. Below is the script (upload.php) that does all the above.
PHP upload file code list C
<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>//getFTPaccessparameters </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>host</SPAN><SPAN>=$_POST['host']; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>user</SPAN><SPAN>=$_POST['user'];$</SPAN><SPAN class=attribute>pass</SPAN><SPAN>=$_POST['pass']; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>destDir</SPAN><SPAN>=$_POST['dir']; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>workDir</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"/usr/local/temp"</SPAN><SPAN>;//definethisasperlocalsystem </SPAN></SPAN><LI class=alt><SPAN>//gettemporaryfilenamefortheuploadedfile </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>tmpName</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>basename</SPAN><SPAN>($_FILES['file']['tmp_name']); </SPAN></SPAN><LI class=alt><SPAN>//copyuploadedfileintocurrentdirectory </SPAN><LI class=""><SPAN>move_uploaded_file($_FILES['file']['tmp_name'],$workDir."/".$tmpName)ordie("Cannotmoveuploadedfiletoworkingdirectory"); </SPAN><LI class=alt><SPAN>//openconnection </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>conn</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>ftp_connect</SPAN><SPAN>($host)ordie("Cannotinitiateconnectiontohost"); </SPAN></SPAN><LI class=alt><SPAN>//sendaccessparameters </SPAN><LI class=""><SPAN>ftp_login($conn,$user,$pass)ordie("Cannotlogin"); </SPAN><LI class=alt><SPAN>//performfileupload </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>upload</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>ftp_put</SPAN><SPAN>($conn,$destDir."/".$_FILES['file']['name'],$workDir."/".$tmpName,FTP_BINARY); </SPAN></SPAN><LI class=alt><SPAN>//checkuploadstatus </SPAN><LI class=""><SPAN>//displaymessage </SPAN><LI class=alt><SPAN>if(!$upload){ </SPAN><LI class=""><SPAN>echo"Cannotupload"; </SPAN><LI class=alt><SPAN>}else{ </SPAN><LI class=""><SPAN>echo"Uploadcomplete"; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>//closetheFTPstream </SPAN><LI class=alt><SPAN>ftp_close($conn); </SPAN><LI class=""><SPAN>//deletelocalcopyofuploadedfile </SPAN><LI class=alt><SPAN>unlink($workDir."/".$tmpName)ordie("Cannotdeleteuploadedfilefromworkingdirectory--manualdeletionrecommended"); </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag>?></span><span> </span></span></li></ol>