下载与卷曲的目录中的所有文件(downloading all the files in a dire

2019-06-26 17:33发布

我使用cURL来尝试下载某个目录中的所有文件。

这里就是我的文件列表的样子:

我曾尝试在bash脚本做: iiumlabs.[].csv.pgpiiumlabs* ,我想袅袅不上通配符大。

curl -u login:pass ftp.myftpsite.com/iiumlabs* -O

问题:我如何下载使用cURL文件的这个目录?

Answer 1:

OK,考虑到你使用的是Windows,最简单的方法是使用标准ftp与它捆绑的工具。 我基地在Windows XP下的解决方案,希望它会在其他版本的工作,以及(或稍作修改)。

首先,您需要创建为一个批次(脚本)文件ftp程序,包含的说明。 它命名为你想要的,并把它付诸表决:

curl -u login:pass ftp.myftpsite.com/iiumlabs* -O

open ftp.myftpsite.com
login
pass
mget *
quit

第一行以打开到FTP服务器的连接ftp.myftpsite.com 。 下面的两个行指定的登录名和FTP协议的密码将要求(更换login ,并pass只用登录名和密码,没有任何关键字)。 然后,使用mget *获得的所有文件。 取而代之的是中* ,你可以使用任何通配符。 最后,使用quit关闭ftp程序没有交互提示。

如果您需要先输入一些目录中,添加一个cd之前命令mget 。 它应该是相当简单的。

最后,编写文件并运行ftp是这样的:

ftp -i -s:yourscript

其中-i禁用交互(下载文件前询问),和-s指定路径创建的脚本。


可悲的是,通过SSH文件传输本身不支持在Windows中。 但是,对于这种情况下,你可能想使用腻子反正工具。 针对这种情况特别感兴趣的人会是pscp这实际上是OpenSSH的的腻子反一部分scp命令。

语法类似于copy命令,它支持通配符:

pscp -batch login@mysshsite.com:iiumlabs* .

如果您使用身份验证密钥文件,你应该使用它传递-i path-to-key-file 。 如果您使用的密码, -pw pass 。 它也可以重用PuTTY的使用保存的会话,使用load -load your-session-name参数。



Answer 2:

如果你没有绑定卷曲,你可能想使用wget的递归模式,但它限制对递归的一个级别,请尝试以下;

wget --no-verbose --no-parent --recursive --level=1\
--no-directories --user=login --password=pass ftp://ftp.myftpsite.com/
  • --no-parent :递归检索时,千万不要上升到父目录。
  • --level=depth :指定递归最大深度级别的深度。 默认的最大深度为五层。
  • --no-directories :递归检索时,不要创建目录的层次结构。


Answer 3:

什么是这样的:

for /f %%f in ('curl -s -l -u user:pass ftp://ftp.myftpsite.com/') do curl -O -u user:pass ftp://ftp.myftpsite.com/%%f


Answer 4:

哦,我正好有你需要的东西!

$host = "ftp://example.com/dir/";
$savePath = "downloadedFiles";
if($check = isFtpUp($host)){

    echo $ip." -is alive<br />";

    $check = trim($check);
    $files = explode("\n",$check);

    foreach($files as $n=>$file){
        $file = trim($file);
        if($file !== "." || $file !== ".."){
            if(!saveFtpFile($file, $host.$file, $savePath)){
                // downloading failed. possible reason: $file is a folder name.
                // echo "Error downloading file.<br />";
            }else{
                echo "File: ".$file." - saved!<br />";
            }
        }else{
            // do nothing
        }
    }
}else{
    echo $ip." - is down.<br />";
}

和功能isFtpUpsaveFtpFile如下:

function isFtpUp($host){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_USERPWD, "anonymous:your@email.com");
curl_setopt($ch, CURLOPT_FTPLISTONLY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);

$result = curl_exec($ch);

return $result;

}

function saveFtpFile( $targetFile = null, $sourceFile = null, $savePath){

// function settings
set_time_limit(60);
$timeout = 60;
$ftpuser = "anonymous";
$ftppassword = "your@email.com";
$savePath = "downloadedFiles"; // should exist!
$curl = curl_init();
$file = @fopen ($savePath.'/'.$targetFile, 'w');

if(!$file){
    return false;
}

curl_setopt($curl, CURLOPT_URL, $sourceFile);
curl_setopt($curl, CURLOPT_USERPWD, $ftpuser.':'.$ftppassword);

// curl settings

// curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_FILE, $file);

$result = curl_exec($curl);


if(!$result){
    return false;
}

curl_close($curl);
fclose($file);

return $result;
}

编辑:

这是一个PHP脚本。 它保存为一个PHP文件,把它放在你的Web服务器,更改IP $解决(不需要IP)的FTP服务器的你想要下载的,创建一个名为放在同一个目录作为该文件downloadedFiles目录中的文件。



Answer 5:

这里是我的表现如何用卷曲快速下载(我不知道有多少文件可以下载,虽然):

setlocal EnableDelayedExpansion

cd where\to\download

set STR=
for /f "skip=2 delims=" %%F in ('P:\curl -l -u user:password ftp://ftp.example.com/directory/anotherone/') do set STR=-O "ftp://ftp.example.com/directory/anotherone/%%F" !STR!
path\to\curl.exe -v -u user:password !STR!

为什么跳过= 2? 要获得乘坐...

为什么delims =? 为了支持空格的名称



Answer 6:

您可以使用这样的脚本为Mac:

for f in $(curl -s -l -u user:pass ftp://your_ftp_server_ip/folder/) 
 do curl -O -u user:pass ftp://your_ftp_server_ip/folder/$f 
done


文章来源: downloading all the files in a directory with cURL