使用 curl 和 php 从 ftp 下载文件 [英] download a file from ftp using curl and php

查看:34
本文介绍了使用 curl 和 php 从 ftp 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 curl 和 php 从 ftp 服务器下载文件,但找不到任何帮助文档

I'm trying to download a file from an ftp server using curl and php but I can't find any documentation to help

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"ftp://$_FTP[server]");
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec ($curl);

我可以获得文件列表,但仅此而已

i can get a list of files but thats about it

推荐答案

我的猜测是您的 URL 指向的是目录,而不是文件.您需要向 CURLOPT_URL 提供文件的完整 URL.此外,如果您想下载文件,您可能希望将其保存在某处.

My guess is that your URL is pointing towards a directory, not a file. You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere.

工作示例:

$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_exec($curl);
curl_close($curl);
fclose($file);

这篇关于使用 curl 和 php 从 ftp 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆