php FTP文件上传示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php FTP文件上传示例相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">// FTP access parameters</span>
<span style="color: #0000ff;">$host</span> = <span style="color: #ff0000;">'ftp.example.org'</span>;
<span style="color: #0000ff;">$usr</span> = <span style="color: #ff0000;">'example_user'</span>;
<span style="color: #0000ff;">$pwd</span> = <span style="color: #ff0000;">'example_password'</span>;
<span style="color: #808080; font-style: italic;">// file to move:</span>
<span style="color: #0000ff;">$local_file</span> = <span style="color: #ff0000;">'./example.txt'</span>;
<span style="color: #0000ff;">$ftp_path</span> = <span style="color: #ff0000;">'/data/example.txt'</span>;
<span style="color: #808080; font-style: italic;">// connect to FTP server (port 21)</span>
<span style="color: #0000ff;">$conn_id</span> = <a href="http://www.php.net/ftp_connect"><span style="color: #000066;">ftp_connect</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$host</span>, <span style="color: #cc66cc;">21</span><span style="color: #66cc66;">)</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Cannot connect to host"</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// send access parameters</span>
<a href="http://www.php.net/ftp_login"><span style="color: #000066;">ftp_login</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$conn_id</span>, <span style="color: #0000ff;">$usr</span>, <span style="color: #0000ff;">$pwd</span><span style="color: #66cc66;">)</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Cannot login"</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// turn on passive mode transfers (some servers need this)</span>
<span style="color: #808080; font-style: italic;">// ftp_pasv ($conn_id, true);</span>
<span style="color: #808080; font-style: italic;">// perform file upload</span>
<span style="color: #0000ff;">$upload</span> = <a href="http://www.php.net/ftp_put"><span style="color: #000066;">ftp_put</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$conn_id</span>, <span style="color: #0000ff;">$ftp_path</span>, <span style="color: #0000ff;">$local_file</span>, FTP_ASCII<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// check upload status:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$upload</span><span style="color: #66cc66;">)</span> ? <span style="color: #ff0000;">'Cannot upload'</span> : <span style="color: #ff0000;">'Upload complete'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #808080; font-style: italic;">/*
** Chmod the file (just as example)
*/</span>
<span style="color: #808080; font-style: italic;">// If you are using PHP4 then you need to use this code:</span>
<span style="color: #808080; font-style: italic;">// (because the "ftp_chmod" command is just available in PHP5+)</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<a href="http://www.php.net/function_exists"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'ftp_chmod'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #000000; font-weight: bold;">function</span> ftp_chmod<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ftp_stream</span>, <span style="color: #0000ff;">$mode</span>, <span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/ftp_site"><span style="color: #000066;">ftp_site</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ftp_stream</span>, <a href="http://www.php.net/sprintf"><span style="color: #000066;">sprintf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'CHMOD %o %s'</span>, <span style="color: #0000ff;">$mode</span>, <span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// try to chmod the new file to 666 (writeable)</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>ftp_chmod<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$conn_id</span>, <span style="color: #cc66cc;">0666</span>, <span style="color: #0000ff;">$ftp_path</span><span style="color: #66cc66;">)</span> !== <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$ftp_path</span> . <span style="color: #ff0000;">" chmoded successfully to 666<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"could not chmod $file<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// close the FTP stream</span>
<a href="http://www.php.net/ftp_close"><span style="color: #000066;">ftp_close</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$conn_id</span><span style="color: #66cc66;">)</span>;
以上是关于php FTP文件上传示例的主要内容,如果未能解决你的问题,请参考以下文章