【发布时间】:2009-12-30 18:34:38
【问题描述】:
我正在尝试使用 PHP 从 FTP 服务器下载文件,我也可以从 http 访问该文件。例如:FTP://username:password@someserver.com/file_name_here.gz。该文件被强制作为来自服务器的附件(而不是 PDF 或 TXT 将执行的操作,例如:输出)。
我尝试了 3 种不同的方法:
- file_get_contents -> 返回一个零大小的文件
- 卷曲 -> 超时
- FTP 函数 -> 返回一个零大小的文件
我认为 CURL 是这样做的方法,但我无法下载文件...这是我的代码:
$curl = curl_init();
$url ='ftp://'.$network['username'].':'.$network['password'].'@'.$network['metadata_url'].'/'.$path.'/'.$data['title'];
curl_setopt($curl, CURLOPT_URL,$url);
//curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$file = fopen("./feeds/".$data['title'], "w");
curl_setopt($curl, CURLOPT_FILE, $file);
$result = curl_exec ($curl);
我尝试在 CURLOPT_URL 中设置用户名和密码,并使用 CURL 设置它们。两种方式都会发生同样的事情......
你能帮忙吗?
Edit: 忘了说文件是从服务器动态提供的。例如,真正的文件是:filename.gz.check,当我省略 .check 部分时,我得到了原始文件。不知道他们为什么这样做,但我需要照原样处理。
【问题讨论】:
-
您可以通过网络浏览器使用FTP://username:password@someserver.com/file_name_here.gz 访问该文件吗?
-
是的,我可以:JeezTech 说:
I am trying to download a file with PHP from an FTP server which i can access from http too.