我需要从其他网站下载图像到我的服务器.使用这些图像创建ZIP文件.自动开始下载创建的ZIP文件.下载完成后,应从我的服务器中删除ZIP文件和图像.
而不是自动下载,下载链接也没关系.但其他逻辑仍然相同.
Pas*_*TIN 22
那么,您必须首先使用ZipArchive该类创建zip文件.
然后,发送:
header()- 该手册的页面上有一个示例应该有帮助readfile()最后,使用从服务器删除zip文件unlink().
注意:作为安全预防措施,最好让PHP脚本自动运行(通常由crontab运行),这将删除临时目录中的旧zip文件.
这是为了防止您的普通PHP脚本被中断,并且不会删除临时文件.
小智 14
<?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();
}
?>
Run Code Online (Sandbox Code Playgroud)