简体   繁体   English

使用php压缩的文件在Windows中不起作用

[英]Compressed files using php won't work in windows

I am trying to compress some video files on the fly using php and forcing compressed file to download. 我正在尝试使用php快速压缩一些视频文件,并强制下载压缩文件。 Below is the code I have written. 以下是我编写的代码。

$files_todownload_string = ' movie1.mov movie2.mov '

header('Content-Type: application/x-gzip');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');
header( 'Content-Transfer-Encoding: binary' ); 

$fp = popen('zip -v - ' . $files_todownload_string, 'r');
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
$buff = fread($fp, $bufsize);
echo $buff;
print "
";
}
pclose($fp);

The compression is done perfectly and the zip file is downloaded successfully. 压缩完美完成,并且zip文件已成功下载。 However, when I am trying to open the zip file in windows os, it won't open. 但是,当我尝试在Windows操作系统中打开zip文件时,它将无法打开。 It says that the compressed file is corrupted. 它说压缩文件已损坏。

Any helps guys? 有帮助吗? Thanks in advance. 提前致谢。

Remove print " "; 删除print " ";

You're adding extra bytes. 您要添加额外的字节。

I found this solution. 我找到了解决方案。 The zipped files are working in all OS Linux, Windows and Mac. 压缩文件可在所有OS Linux,Windows和Mac上使用。

Below is code: 下面是代码:

$files_array = array();
$files_array[] = 'file1.mov';
$files_array[] = 'file2.mov';
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
foreach($files_array as $k => $v) {
$zip->addFile( $path.$files_available[$v] );
}
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
header( 'Content-Transfer-Encoding: binary' ); 
ob_end_clean();
readfile($file);

Thanks everybody for giving the time. 感谢大家抽出宝贵的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM