我需要從其他網站下載圖像到我的服務器。用這些圖像創建一個ZIP文件。自動開始下載創建的ZIP文件。一旦下載完成,ZIP文件和圖像應該從我的服務器上刪除。如何使用PHP創建ZIP文件並在用戶下載後將其刪除?
而不是自動下載,下載鏈接也很好。但其他邏輯保持不變。
我需要從其他網站下載圖像到我的服務器。用這些圖像創建一個ZIP文件。自動開始下載創建的ZIP文件。一旦下載完成,ZIP文件和圖像應該從我的服務器上刪除。如何使用PHP創建ZIP文件並在用戶下載後將其刪除?
而不是自動下載,下載鏈接也很好。但其他邏輯保持不變。
那麼,你必須先使用ZipArchive
類創建zipfile。
的話,就送:
header()
- 有那個手冊頁上的例子,應該幫助readfile()
而且,最後的內容,刪除服務器的zip文件,使用unlink()
。
注:爲安全起見,它可能是明智的,有一個PHP腳本會自動運行(由crontab中,通常情況下),這將刪除您的臨時目錄中的舊zip文件。
這是爲了防止您的普通PHP腳本有時會中斷並且不會刪除臨時文件。
任何想法有多少zip文件下載被中斷,需要繼續?
如果繼續下載只是下載的一小部分,您可以立即刪除zip文件;只要您的服務器仍然將文件發送到客戶端,它將保留在磁盤上。
服務器關閉文件描述符後,文件的引用計數將降爲零,最後磁盤上的塊將被釋放。
但是,如果許多下載被中斷,您可能會花費相當多的時間重新創建zip文件。好的便宜優化如果你可以擺脫它。
謝謝,這是很好的信息,真的是解決問題所需的唯一信息。不知道爲什麼不多投票。 – Bryan 2015-09-06 02:12:55
以下是我過去能夠做到的。此代碼假定您已將文件寫入由$path
變量指定的路徑。您可能需要處理您的服務器配置一些權限問題與使用PHP的exec
// write the files you want to zip up
file_put_contents($path . "/file", $output);
// zip up the contents
chdir($path);
exec("zip -r {$name} ./");
$filename = "{$name}.zip";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode($filename));
header('Content-Transfer-Encoding: binary');
readfile($filename);
<?php
Zip('some_directory/','test.zip');
if(file_exists('test.zip')){
//Set Headers:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime('test.zip')) . ' GMT');
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="test.zip"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('test.zip'));
header('Connection: close');
readfile('test.zip');
exit();
}
if(file_exists('test.zip')){
unlink('test.zip');
}
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
?>
該zip文件將永遠不會被刪除。 – Sven 2012-10-19 23:00:53
由於您不知道下載何時完成,因此從代碼中刪除zip文件是不可能的。真的,你需要通過cron作業來刪除文件。 – 2013-10-30 16:58:00
難道你不能將zip文件讀入字符串,然後刪除文件?當然,只有相對較小的文件,但它仍然是一個體面的方法。 – user1111929 2017-07-30 23:24:09
啓用您的php_curl擴展; (php.ini),然後使用下面的代碼創建zip。 創建一個文件夾類,並使用如下代碼:
<?php
include("class/create_zip.php");
$create_zip = new create_zip();
//$url_path,$url_path2 you can use your directory path
$urls = array(
'$url_path/file1.pdf',
'$url_path2/files/files2.pdf'
); // file paths
$file_name = "vin.zip"; // zip file default name
$file_folder = rand(1,1000000000); // folder with random name
$create_zip->create_zip($urls,$file_folder,$file_name);
$create_zip->delete_directory($file_folder); //delete random folder
if(file_exists($file_name)){
$temp = file_get_contents($file_name);
unlink($file_name);
}
echo $temp;
?>
創建一個文件夾類,並使用如下代碼:
<?php
class create_zip{
function create_zip($urls,$file_folder,$file_name){
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
header('Content-Transfer-Encoding: binary');
$mkdir = mkdir($file_folder);
$zip = new ZipArchive;
$zip->open($file_name, ZipArchive::CREATE);
foreach ($urls as $url)
{
$path=pathinfo($url);
$path = $file_folder.'/'.$path['basename'];
$zip->addFile($path);
$fileopen = fopen($path, 'w');
$init = curl_init($url);
curl_setopt($init, CURLOPT_FILE, $fileopen);
$data = curl_exec($init);
curl_close($init);
fclose($fileopen);
}
$zip->close();
}
function delete_directory($dirname)
{
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle))
{
if ($file != "." && $file != "..")
{
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
}
?>
其他解決方案:之前創建新的zip文件中刪除過去的文件:
// Delete past zip files script
$files = glob('*.zip'); //get all file names in array
$currentTime = time(); // get current time
foreach($files as $file){ // get file from array
$lastModifiedTime = filemtime($file); // get file creation time
// get how old is file in hours:
$timeDiff = abs($currentTime - $lastModifiedTime)/(60*60);
//check if file was modified before 1 hour:
if(is_file($file) && $timeDiff > 1)
unlink($file); //delete file
}
到目前爲止,您的嘗試有哪些?堆棧溢出不在這裏爲您提供源代碼。你需要嘗試自己解決這個問題。如果您遇到特定的問題/問題,請在首次檢查後查看是否有其他用戶尚未提出相同問題,然後發佈有針對性的問題。 – 2011-04-09 08:40:50