我有一個ZIPS目錄的腳本,但是還需要添加* .php文件。RecursiveIteratorIterator:在zip中包含目錄和php文件
當添加諸如../index.php之類的東西時會引發錯誤。通過下面的腳本生成
錯誤是:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(../index.php): failed to open dir: Not a directory' in /home/mathtest/public_html/trig/admin/save.php:23 Stack trace: #0 /home/mathtest/public_html/trig/admin/save.php(23): RecursiveDirectoryIterator->__construct('../index.php') #1 {main} thrown in /home/mathtest/public_html/trig/admin/save.php on line 23
我的腳本:
<?php
/* CONFIG */
$pathToAssets = array("../images", "../Data", "../css", "../index.php");
$filename = "temp/backup.zip";
/* END CONFIG */
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE);
//add folder structure
foreach ($pathToAssets as $thePath) {
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($thePath), RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if ($file->getFilename() != '.' && $file->getFilename() != '..') {
// Get real path for current file
$filePath = $file->getRealPath();
$temp = explode("/", $name);
array_shift($temp);
$newName = implode("/", $temp);
// Add current file to archive
$zip->addFile($filePath, $newName);
}
}
}
$zip->close();
$yourfile = $filename;
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
unlink('temp/backup.zip');
exit;
?>
我在這裏http://php.net/manual/en/class.recursiveiteratoriterator.php也有許多問題沒有運氣解決讀到RecursiveIteratorIterator。
替換../index.php只是../作品,但包括不希望放在zip中的目錄。
任何允許在下載的zip中插入php的輸入值得讚賞。
RecursiveDirectoryIterator只會掃描目錄。所以,你可能需要單獨處理文件,或者你也可以使你的迭代器適應包含文件。 Thriault的https://secure.php.net/manual/en/class.recursivedireiteiterator.php上的第一條評論涉及php文件。 – jedifans
謝謝@jedifans。我曾嘗試過B4發佈,但無法正常工作。可能是我沒有適應我的腳本正確的獲取zip文件中沒有PHP文件。 – Woody