Maison >développement back-end >tutoriel php >三步FTP实现PHP上传文件代码剖析_PHP教程
想知道上传文件的详细步骤吗,下面我就带大家一起详细分析一下吧。利用PHP,你总是可以有多种方式来完成某个特定的任务。我们就拿文件上传举个例子。当然了,你可以按照传统的方式来使用HTTP文件上传,把文件直接传输到Web服务器磁盘上。你还可以用更加奇异的方式上传,用FTP协议两步就完成上传:从你的本地硬盘到Web服务器,然后再到FTP服务器。
第一步:确信你拥有连接/上传到FTP服务器的权限
PHP的FTP函数需要客户端-服务器连接,所以你需要在进行文件上传之前登录到目标服务器上。你的第一项任务是确信你已经拥有了完成这项任务的信任书。这一步可能看起来是理所当然的,但是你会惊奇地发现有多少开发人员忘了这么做,结果后来浪费大量的时间来解决因此而出现的问题。你可以通过使用命令行的FTP客户端登录到目标服务器上,以检查连接状况并尝试上传一个假文件。
PHP上传文件代码列表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>
一旦确认有相关的访问权限,你就可以登录出来。
第二步:创建上传表单
然后,创建一个简单的HTML表单,向用户询问重要的参数——FTP服务器的访问信息、服务器上传的目录,以及完整的目录和上传文件的名字。下面这个例子就是这个表单的样式。
PHP上传文件代码列表B
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">h2</span><span class="tag">></span></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 class="tag-name">br</span><span class="tag">/></span></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">/></span><span class="tag"><span class="tag-name">p</span><span class="tag">/></span></span></font></strong><span> </span> </li> <li class=""> <span>Username</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">/></span></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">/></span><span class="tag"><span class="tag-name">p</span><span class="tag">/></span></span></font></strong><span> </span> </li> <li class=""> <span>Password</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">/></span></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">/></span><span class="tag"><span class="tag-name">p</span><span class="tag">/></span></span></font></strong><span> </span> </li> <li class=""> <span>Destinationdirectory</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">/></span></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">/></span><span class="tag"><span class="tag-name">p</span><span class="tag">/></span></span></font></strong><span> </span> </li> <li class=""> <span>File</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">/></span></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">/></span><span class="tag"><span class="tag-name">p</span><span class="tag">/></span></span></font></strong><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>
在这里,
第三步:创建PHP上传处理程序
一旦表单被提交给了Web服务器,下一步(也是最后一步)是使用PHP的FTP函数按照用户提供的访问信任书把它传输到目标服务器上。下面就是完成上述所有工作的脚本(upload.php)。
PHP上传文件代码列表C
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li class=""><span>//getFTPaccessparameters </span></li> <li class="alt"> <span>$</span><span class="attribute">host</span><span>=$_POST['host']; </span> </li> <li class=""> <span>$</span><span class="attribute">user</span><span>=$_POST['user'];$</span><span class="attribute">pass</span><span>=$_POST['pass']; </span> </li> <li class="alt"> <span>$</span><span class="attribute">destDir</span><span>=$_POST['dir']; </span> </li> <li class=""> <span>$</span><span class="attribute">workDir</span><span>=</span><span class="attribute-value">"/usr/local/temp"</span><span>;//definethisasperlocalsystem </span> </li> <li class="alt"><span>//gettemporaryfilenamefortheuploadedfile </span></li> <li class=""> <span>$</span><span class="attribute">tmpName</span><span>=</span><span class="attribute-value">basename</span><span>($_FILES['file']['tmp_name']); </span> </li> <li class="alt"><span>//copyuploadedfileintocurrentdirectory </span></li> <li class=""><span>move_uploaded_file($_FILES['file']['tmp_name'],$workDir."/".$tmpName)ordie("Cannotmoveuploadedfiletoworkingdirectory"); </span></li> <li class="alt"><span>//openconnection </span></li> <li class=""> <span>$</span><span class="attribute">conn</span><span>=</span><span class="attribute-value">ftp_connect</span><span>($host)ordie("Cannotinitiateconnectiontohost"); </span> </li> <li class="alt"><span>//sendaccessparameters </span></li> <li class=""><span>ftp_login($conn,$user,$pass)ordie("Cannotlogin"); </span></li> <li class="alt"><span>//performfileupload </span></li> <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> </li> <li class="alt"><span>//checkuploadstatus </span></li> <li class=""><span>//displaymessage </span></li> <li class="alt"><span>if(!$upload){ </span></li> <li class=""><span>echo"Cannotupload"; </span></li> <li class="alt"><span>}else{ </span></li> <li class=""><span>echo"Uploadcomplete"; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>//closetheFTPstream </span></li> <li class="alt"><span>ftp_close($conn); </span></li> <li class=""><span>//deletelocalcopyofuploadedfile </span></li> <li class="alt"><span>unlink($workDir."/".$tmpName)ordie("Cannotdeleteuploadedfilefromworkingdirectory--manualdeletionrecommended"); </span></li> <li class=""> <span></span><span class="tag">?></span><span> </span> </li> </ol>