编写文件然后压缩文件并下载提交

时间:2017-07-04 11:37:01

标签: php file download zip

我正在尝试创建文件(工作),压缩文件(工作)和下载。但是,当我按照我认为正确的顺序进行操作时,我会收到警告,说明标头已经发送

我的代码是:

//TOP Writes to file -this works
$handle=fopen('db_backup.sql','w+');
fwrite($handle,$end);
fclose($handle);

//Creates zip -this works
$zip   =array('db_backup.sql');
create_zip($zip,'db_backup.zip',true);
ob_clean();
flush();

//Download the zip get the headers already sent message
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=db_backup.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize('db_backup.zip'));
readfile('db_backup.zip');
unlink($zip_file);

但如果我将标题移到TOP,我就不会收到消息。拉链是空的!

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=db_backup.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize('db_backup.zip'));

//TOP Writes to file -this works
$handle=fopen('db_backup.sql','w+');
fwrite($handle,$end);
fclose($handle);

//Creates zip -this works
$zip   =array('db_backup.sql');
create_zip($zip,'db_backup.zip',true);
ob_clean();
flush();

//Download the zip get the headers already sent message

readfile('db_backup.zip');
unlink($zip_file);

0 个答案:

没有答案