想知道上传文件的详细步骤吗,下面我就带大家一起详细分析一下吧。利用php,你总是可以有多种方式来完成某个特定的任务。我们就拿文件上传举个例子。当然了,你可以按照传统的方式来使用http文件上传,把文件直接传输到web服务器磁盘上。你还可以用更加奇异的方式上传,用ftp协议两步就完成上传:从你的本地硬盘到web服务器,然后再到ftp服务器。
第一步:确信你拥有连接/上传到FTP服务器的权限
PHP的FTP函数需要客户端-服务器连接,所以你需要在进行文件上传之前登录到目标服务器上。你的第一项任务是确信你已经拥有了完成这项任务的信任书。这一步可能看起来是理所当然的,但是你会惊奇地发现有多少开发人员忘了这么做,结果后来浪费大量的时间来解决因此而出现的问题。你可以通过使用命令行的FTP客户端登录到目标服务器上,以检查连接状况并尝试上传一个假文件。
PHP上传文件代码列表A
立即学习“PHP免费学习笔记(深入)”;
<OL class=dp-xml><LI class=alt><SPAN><SPAN>$ftp </SPAN></SPAN><LI class=""><SPAN>ftp</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>opensome.host.com </SPAN></SPAN><LI class=alt><SPAN>Connectedtosome.host.com. </SPAN><LI class=""><SPAN>220WelcometoleonFTPServer! </SPAN><LI class=alt><SPAN>User:upload </SPAN><LI class=""><SPAN>331Useruploadokay,needpassword. </SPAN><LI class=alt><SPAN>Password:*** </SPAN><LI class=""><SPAN>230Restricteduserloggedin. </SPAN><LI class=alt><SPAN>ftp</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>bin </SPAN></SPAN><LI class=""><SPAN>200Typeokay. </SPAN><LI class=alt><SPAN>ftp</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>hash </SPAN></SPAN><LI class=""><SPAN>HashmarkprintingOn?ftp:(2048bytes/hashmark). </SPAN><LI class=alt><SPAN>ftp</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>putfile.bin </SPAN></SPAN><LI class=""><SPAN>200PORTcommandsuccessful. </SPAN><LI class=alt><SPAN>150OpeningBINARYmodedataconnection. </SPAN><LI class=""><SPAN>## </SPAN><LI class=alt><SPAN>226Transfercompleted. </SPAN><LI class=""><SPAN>ftp:4289bytessentin0.00Seconds4289000.00Kbytes/sec. </SPAN><LI class=alt><SPAN>ftp</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>bye </SPAN></SPAN><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><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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></SPAN><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></SPAN><LI class=alt><SPAN> </SPAN><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></SPAN><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></SPAN><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></SPAN><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><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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><SPAN class=tag-name>p</SPAN><SPAN class=tag>/></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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></SPAN><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></SPAN><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></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 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>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号