Home > php教程 > php手册 > body text

从ftp服务器上下载文件

WBOY
Release: 2016-06-13 10:59:20
Original
1148 people have browsed it

<?php <P>/**</P><P>* 函数名 php_ftp_download</P><P>* 功能 从Ftp服务器上下载文件</P><P>* 入口参数</P><P>* filename 欲下载的文件名,含路径</P><P>*/</P><P>function php_ftp_download($filename) {</P><P>$phpftp_host = "ftplocalhost"; // 服务器地址</P><P>$phpftp_port = 21; // 服务器端口</P><P>$phpftp_user = "name"; // 用户名</P><P>$phpftp_passwd = "passwrd"; // 口令</P><P>$ftp_path = dirname($filename) . "/"; // 获取路径</P><P>$select_file = basename($filename); // 获取文件名</P><P>$ftp = ftp_connect($phpftp_host,$phpftp_port); // 连接Ftp服务器</P><P>if($ftp) {</P><P>if(ftp_login($ftp, $phpftp_user, $phpftp_passwd)) { // 登录</P><P>if(@ftp_chdir($ftp,$ftp_path)) { // 进入指定路径</P><P>$tmpfile = tempnam( getcwd()."/", "temp" ); // 创建唯一的临时文件</P><P>if(ftp_get($ftp, $tmpfile, $select_file, FTP_BINARY)) { // 下载指定的文件到临时文件</P><P>ftp_quit( $ftp ); // 关闭连接</P><P>header("Content-Type: application/octet-stream");</P><P>header("Content-Disposition: attachment; filename=" . $select_file);</P><P>readfile($tmpfile);</P><P>unlink($tmpfile ); // 删除临时文件</P><P>exit;</P><P>}</P><P>unlink($tmpfile );</P><P>}</P><P>}</P><P>}</P><P>ftp_quit($ftp);</P><P>}</P><P>?></P><P>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!