我想打包一个文件夹,然后下载这个。打包的文件已经创建,但是在文件名后面附加了一个代码,所以当我调用该文件进行下载时,它并不存在,因为“物理上”它们有另一个名称。遵循以下代码:
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('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file));
}
else if (is_file($file) === true)
{
$str1 = str_replace($source . '/', '', '/'.$file);
$zip->addFromString($str1, file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
$zip->close();
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($destination));
header('Connection: close');
readfile($destination);
exit();
}readfile($file_name)正在调用filename.zip,但实际名称是filename.zip.06452,所以我得到了一个错误。
发布于 2015-01-27 21:58:30
您应该:
header('Content-Length: ' . filesize($destination));和
readfile($destination);因为那是你写归档文件的地方:
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
...发布于 2015-01-28 01:51:36
我有办法了!这个文件的末尾有代码,myfile.zip.das87是一个临时文件,它是php在启动打包过程时创建的,完成后将其重命名为myfile.zip。我打印了错误消息并得到了Renaming temporary file failed,所以问题出在文件夹权限上。感谢所有试图帮助我的人。
https://stackoverflow.com/questions/28172188
复制相似问题