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>;
&nbsp;
<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>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$host</span>, <span style="color: #cc66cc;">21</span><span style="color: #66cc66;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot connect to host&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$conn_id</span>, <span style="color: #0000ff;">$usr</span>, <span style="color: #0000ff;">$pwd</span><span style="color: #66cc66;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot login&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<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>
&nbsp;
<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;">&#40;</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;">&#41;</span>;
&nbsp;
<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;">&#40;</span>!<span style="color: #0000ff;">$upload</span><span style="color: #66cc66;">&#41;</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;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Chmod the file (just as example)
*/</span>
&nbsp;
<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 &quot;ftp_chmod&quot; command is just available in PHP5+)</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/function_exists"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ftp_chmod'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">function</span> ftp_chmod<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ftp_stream</span>, <span style="color: #0000ff;">$mode</span>, <span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</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;">&#40;</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;">&#40;</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;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<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;">&#40;</span>ftp_chmod<span style="color: #66cc66;">&#40;</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;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</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;">&quot; chmoded successfully to 666<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;could not chmod $file<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$conn_id</span><span style="color: #66cc66;">&#41;</span>;

以上是关于php FTP文件上传示例的主要内容,如果未能解决你的问题,请参考以下文章

怎么通过PHP来支持FTP和HTTP上传

php POST上传和FTP上传哪个好 为啥

PHP使用FTP上传文件到服务器(实战篇)

PHP使用FTP上传文件到服务器(实战篇)

ftp上传单一文件示例

ftp上传单一文件示例