在与PHP脚本相同的服务器上下载文件数组

在与PHP脚本相同的服务器上下载文件数组,php,Php,我有一个阵列或1000多个文件需要从服务器下载到本地PC 我需要保留它,以便为每个文件复制相同的文件/文件夹结构 以下是文件的示例列表: /lib/Zend/EventManager/Filter/FilterIterator.php /lib/Zend/EventManager/config.php /lib/Zend/Text/Figlet/themes.php /lib/Zend/Gdata/Analytics/DataEntry.php /lib/Zend/Gdata/Analytics

我有一个阵列或1000多个文件需要从服务器下载到本地PC

我需要保留它,以便为每个文件复制相同的文件/文件夹结构

以下是文件的示例列表:

/lib/Zend/EventManager/Filter/FilterIterator.php
/lib/Zend/EventManager/config.php
/lib/Zend/Text/Figlet/themes.php
/lib/Zend/Gdata/Analytics/DataEntry.php
/lib/Zend/Gdata/Analytics/AccountQuery.php
/lib/Zend/Gdata/Calendar/files.php
/lib/Zend/Gdata/Query.php
/lib/Zend/Gdata/Gbase/Feed.php
/lib/Zend/Gdata/Photos.php
/lib/Zend/Gdata/Photos/AlbumFeed.php
/lib/Zend/Gdata/Media/Extension/press.php
/lib/Zend/Gdata/Media/file.php
/lib/Zend/Gdata/Extension/RecurrenceException.php
/lib/Zend/Gdata/Extension/Comments.php
/lib/Zend/Gdata/Extension/Recurrence.php
/lib/Zend/Gdata/Extension/Rating.php
要创建文件夹,请使用FTP导航到它们,然后下载它们将花费我一整天的时间!如何使用PHP实现这一点

无法使用URL在浏览器中访问这些文件,因此我必须使用文件路径


更新

以下是我迄今为止使用PHP ZipArchive所做的尝试

files.txt
测试文件以测试我将需要的文件样本。最终结果将超过1000个文件

lib/Zend/EventManager/Filter/FilterIterator.php
lib/Zend/EventManager/config.php
lib/Zend/Text/Figlet/themes.php
lib/Zend/Gdata/Analytics/DataEntry.php
lib/Zend/Gdata/Analytics/AccountQuery.php
lib/Zend/Gdata/Calendar/files.php
lib/Zend/Gdata/Query.php
lib/Zend/Gdata/Gbase/Feed.php
lib/Zend/Gdata/Photos.php
lib/Zend/Gdata/Photos/AlbumFeed.php
lib/Zend/Gdata/Media/Extension/press.php
lib/Zend/Gdata/Media/file.php
lib/Zend/Gdata/Extension/RecurrenceException.php
lib/Zend/Gdata/Extension/Comments.php
lib/Zend/Gdata/Extension/Recurrence.php
lib/Zend/Gdata/Extension/Rating.php
lib/Zend/Gdata/Books/VolumeQuery.php
lib/Zend/Gdata/Books/VolumeFeed.php
lib/Zend/Gdata/Exif/themes.php
lib/Zend/Gdata/MimeBodyString.php
lib/Zend/Gdata/HttpAdapterStreamingProxy.php
lib/Zend/Gdata/Spreadsheets/Extension/test.php
lib/Zend/Gdata/Spreadsheets/ListEntry.php
lib/Zend/Gdata/Gapps/Query.php
lib/Zend/Gdata/Gapps/GroupQuery.php
lib/Zend/Gdata/Gapps/EmailListRecipientQuery.php
lib/Zend/Gdata/Gapps/Error.php
lib/Zend/Gdata/Gapps/OwnerFeed.php
lib/Zend/Gdata/Gapps/alias.php
lib/Zend/Gdata/Gapps/MemberQuery.php
lib/Zend/Gdata/Gapps/EmailListQuery.php
lib/Zend/Gdata/Gapps/NicknameFeed.php
lib/Zend/Gdata/Exif.php
lib/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php
lib/Zend/Gdata/App/Extension.php
lib/Zend/Gdata/App/MediaEntry.php
lib/Zend/Gdata/App/FeedEntryParent.php
lib/Zend/Gdata/App/AuthException.php
下载.php

$zip = new ZipArchive();
$filename = "./test112.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}else{
    echo 'zip good';
}
//$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
//$zip->addFile("lib/Zend/files2.txt" ,"lib/Zend/EventManager/test.php" );

// list of files to download
$lines = file('files.txt');

// Loop through our array of files from the files.txt file
foreach ($lines as $line_num =>$file) {
    //echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($file) . "<br />\n";

    // Add files to Zip file incliuding folder structure
    $zip->addFile($file,$file);

    echo $file;
}

// show number of files in new zip file and close zip archive
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
$zip=new ZipArchive();
$filename=“./test112.zip”;
如果($zip->open($filename,ZipArchive::CREATE)!==TRUE){
退出(“无法打开”);
}否则{
回音“zip good”;
}
//$zip->addFromString(“testfilephp.txt.time(),”#1这是作为testfilephp.txt添加的测试字符串。\n”);
//$zip->addFile(“lib/Zend/files2.txt”、“lib/Zend/EventManager/test.php”);
//要下载的文件列表
$lines=file('files.txt');
//循环浏览files.txt文件中的文件数组
foreach($line作为$line\u num=>$file的行){
//echo“Line#{$Line_num}:”.htmlspecialchars($file)。“
\n”; //将文件添加到包含文件夹结构的Zip文件 $zip->addFile($file,$file); echo$文件; } //显示新zip文件中的文件数并关闭zip存档 回声“numfiles:”$zip->numFiles。“\n”; 回显“状态:”$压缩->状态。“\n”; $zip->close();
结果

$zip = new ZipArchive();
$filename = "./test112.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}else{
    echo 'zip good';
}
//$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
//$zip->addFile("lib/Zend/files2.txt" ,"lib/Zend/EventManager/test.php" );

// list of files to download
$lines = file('files.txt');

// Loop through our array of files from the files.txt file
foreach ($lines as $line_num =>$file) {
    //echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($file) . "<br />\n";

    // Add files to Zip file incliuding folder structure
    $zip->addFile($file,$file);

    echo $file;
}

// show number of files in new zip file and close zip archive
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

这将创建我的zip文件,但是它没有添加所有文件,而是将我的文件数组中的最后一个文件添加到zip存档!在本例中,即
lib/Zend/Gdata/App/AuthException.php

由于您具有SSH访问权限,您只需在服务器上运行以下操作:

# Change '*.php' to whatever you want to retrieve:
find . -name '*.php' -print | zip archive.zip -@

然后,您可以通过scp或ftp获取archive.zip文件。

为什么不在服务器上压缩整个文件夹,然后下载归档文件?这就是“许多文件”通常被传输的方式,在互联网上…@Siguza整个服务器文件将是多GB和数千个我不需要的文件。我只需要每个文件夹中的某些文件。这是不实际的。你有SSH访问服务器的权限吗?因为制作一个简单的脚本来查找和gzip文件会更容易。@this.lau\ui确实有SSH访问权限,所以制作一个脚本来迭代数组,并在zip中包含每个迭代的文件。您可以使用php创建zip文件。David Wash可以帮助您:有没有办法定义我需要的文件列表和文件夹结构?我有一个包含大约1000个文件的PHP数组,其中包含文件夹路径和文件名。您可以使用“-name”参数定义所需的文件。如果您需要多个标准。您可以使用-o:
find-名称'*.php'-o-name'*.css'
。或者你的意思是你已经有了文件列表?在这种情况下,您可以跳过find命令:
cat yourlist.txt | zip archive.zip-@
。是的,我有一个包含1000个文件的列表,其中每一个文件的路径和名称位于一个新行。我会尝试你的新答案,谢谢