我的 FTP 站点上有文件(仅限本地)。我想将这些文件打包为 ZIP 格式并下载。
可以使用 PHP 来做到这一点吗?
请访问此链接:https://gist.github.com/jonmaim/1141513/7a90a2c6661f813ae1b99426ec205e8666505b00
你可以像这样压缩你的服务器。
<?php
$filesToZip = array();
// Connect to FTP server
$ftpServer = "localhost:8080";
$ftpUsername = "zzz";
$ftpPassword = "pass";
$ftpConnection = ftp_connect($ftpServer);
ftp_login($ftpConnection, $ftpUsername, $ftpPassword);
$ftpDirectory = "/path/to/your/files";
$files = ftp_nlist($ftpConnection, $ftpDirectory);
foreach ($files as $file) {
$filesToZip[] = $file;
}
ftp_close($ftpConnection);
// Create a ZIP file
$zip = new ZipArchive();
$zipFileName = "downloaded_files.zip";
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
foreach ($filesToZip as $file) {
$fileContent = ftp_get($ftpConnection, 'php://temp', $file, FTP_BINARY);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFileName);
header('Content-Length: ' . filesize($zipFileName));
readfile($zipFileName);
unlink($zipFileName);
} else {
echo "Failed to create ZIP file.";
}
?>