これは、フォルダー全体の .zip を作成してダウンロードすることを目的とした、インターネットで見つけたコードです。
いくつかの変更を加えたいのですが、試したすべてが機能しません。私が欲しいもの:
汎用的にします (つまり、それ
makezip.php
をルート フォルダーに置きたいのです。ファイル マネージャーがあり、このスクリプトをどこかの場所から呼び出すたびに (たとえば、www.domain.com/files/media/images/
そのフォルダーを取得します)。ダウンロード後にzipを削除します(コマンドを使用してうまくやったと思いますが、それ
unlink
が正しい方法かどうかはわかりません.それとzipが取得するフォルダーも削除し
.
ます。..
zip を名前
date.hour.directory.zip
(ex18Sep2013_13-26-02_images.zip
) でエクスポートします。私はこれをやろうとしまし$fd = getcwd();
$filename = date("dMY_H:i:s").'_'.$fd.'.zip';
たが、うまくいきませんでした.$fd変数しか取得しません.
すべてのコードを提供することになっていないことはわかっており、それを調べたりデバッグしたりすることを期待していますが、すべてを試しましたが、何も機能しません。私は独学で学んでおり、PHPの初心者です。
よろしくお願いします
<?php
function download($file) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
$directory = '.';
$files = scandir($directory);
if(empty($files))
{
echo("You haven't selected any file to download.");
}
else
{
$zip = new ZipArchive();
$filename = date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
exit("Cannot open <$filename>\n");
}
$N = count($files);
for($i=0; $i < $N; $i++)
{
$zip->addFile($files[$i], $files[$i]); //add files to archive
}
$numFiles = $zip->numFiles;
$zip->close();
$time = 8; //how long in seconds do we wait for files to be archived.
$found = false;
for($i=0; $i<$time; $i++){
if($numFiles == $N){ // check if number of files in zip archive equals number of checked files
download($filename);
$found = true;
break;
}
sleep(1); // if not found wait one second before continue looping
}
if($found) { }
else echo "Sorry, this is taking too long";
}
?>