Php 编写文件,然后压缩文件并在提交时下载
我正在尝试创建一个文件(works),压缩一个文件(works),然后下载。但是,当我把它按我认为正确的顺序排列时,我会收到一条警告,说标题已经发送了 我的代码是:Php 编写文件,然后压缩文件并在提交时下载,php,file,download,zip,Php,File,Download,Zip,我正在尝试创建一个文件(works),压缩一个文件(works),然后下载。但是,当我把它按我认为正确的顺序排列时,我会收到一条警告,说标题已经发送了 我的代码是: //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');
//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);
但是如果我把标题移到顶部,我就不会得到消息。但是拉链是空的
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);
脚本的完整输出是什么,是否有任何警告?在文件的开始标记之前是否有任何字符?您是否将脚本保存为UTF-8(尝试ASCII)?当您说脚本时,这是否意味着创建了文件?