次のzipダウンロード機能があります。
$file='myStuff.zip';
function downloadZip($file){
$file=$_SERVER["DOCUMENT_ROOT"].'/uploads/'.$file;
if (headers_sent()) {
echo 'HTTP header already sent';
}
else {
if (!is_file($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
exit;
}
}
}
問題は、この関数を呼び出すと、myStuff.zip だけでなく、すべてのフォルダーを含む完全なディレクトリ パスをダウンロードしてしまうことです。XAMPPを使用しているMacを使用しているため、次のようになります。
/applications/xampp/htdocs/uploads/myStuff.zip
つまり、すべてのサブフォルダーを含む applications というフォルダーを取得し、それらすべての中に myStuff.zip を取得します。
myStuff.zip
ディレクトリなしでダウンロードするにはどうすればよいですか?