php中的Zip下载问题

php中的Zip下载问题,php,download,zip,Php,Download,Zip,if(isset($\u请求['create\u all'])) { $app_id=$_请求['app']; $fullpath=array(); $dir=dirname(文件名); foreach($language\u数组为$key\u lang=>$name\u lang) { $path=$dir.“/upload_xml/”$app_id./”; 如果(!is_dir($path)) { mkdir($path,0777); chmod($path,0755); } $QUERY=

if(isset($\u请求['create\u all']))
{
$app_id=$_请求['app'];
$fullpath=array();
$dir=dirname(文件名);
foreach($language\u数组为$key\u lang=>$name\u lang)
{
$path=$dir.“/upload_xml/”$app_id./”;
如果(!is_dir($path))
{
mkdir($path,0777);
chmod($path,0755);
}
$QUERY=mysql\u查询("选择a.process_标志、a.field_id、a.field_名称、a.xml_状态、b.*、c.process_标志、b.lang_id从app_lang_fieldmaster_新建a左连接app_lang_TranslateText_新建b.field_id=b.field_id左连接app_lang_用户_状态c.field id=c.field_id左连接app_明智的_字段d在a.field_id=d.field上,其中a.process_='0'和c='6'd b.lang_id='“$key_lang.”和a.xml_status='1'和a.module_master_id='1'和d.app_id='“$app_id.”和“$groupby 2”,$cont);
//创建xml文档
$xmlDoc=新的DOMDocument(“1.0”、“UTF-8”);
//创建根元素
$root=$xmlDoc->appendChild(
$xmlDoc->createElement(“资源”);
while($row=mysql\u fetch\u object($QUERY))
{
如果($key_lang==9)
{
$xml_string=$xmlDoc->createElement(“string”,trim($row->translate,”);
$xml\u string->setAttribute(“名称”,$row->field\u名称);
$root->appendChild($xml\u字符串);
}
其他的
{
$xml_string=$xmlDoc->createElement(“string”,trim(base64_解码($row->translate),”);
$xml\u string->setAttribute(“名称”,$row->field\u名称);
$root->appendChild($xml\u字符串);
}
}
标题(“内容类型:文本/普通;字符集=ISO-8859-15”);
//使输出美观
$xmlDoc->formatOutput=true;
$string=$xmlDoc->saveXML();
$handle=fopen($path.$name_lang。“.xml”,“w”);
fwrite($handle,$string);
fclose($handle);
//$string=$xmlDoc->save($path.$name_lang。“.xml”);
$fullpath[$key\u lang]=$path.$name\u lang.“.xml”;
}
ob_clean();
冲洗();
//zip文件创建
$zip=新的ZipArchive();
//压缩文件名
$FilePath=$app_id.“.zip”;
$full_path_zip='https://'。$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])。“/”$FilePath;
//检查文件是否存在,如果存在,则删除
如果(文件存在(dirname(uuuu文件)。“/upload_xml/”$FilePath))
{
取消链接(dirname(uuu文件)“/upload_xml/”$FilePath);
}
//打开zip文件以将文件添加到zip文件
如果($zip->open(dirname(\uuuuuuuuuuu文件)。“/upload\uxml/”$FilePath,ZIPARCHIVE::CREATE)!=TRUE)
{
死亡(“无法打开档案”);
}
//$zip->addFile(“文件路径”、“文件名”);
//循环浏览每个文件
foreach($key=>name的语言数组)
{
//将文件添加到zip
$zip->addFile($fullpath[$key],$name..xml”);
}
//关闭并保存存档
$zip->close();
//收割台_移除();
echo$full_path_zip;
ob_clean();
冲洗();
$filename=dirname(_文件)。“/upload_xml/”$FilePath;
标头('Cache-Control:public');
标题(“内容描述:文件传输”);
标题(“内容类型:应用程序/zip”);
标题('Content-Disposition:attachment;filename=“.”.$filename.'”);
//echo目录名(_文件).“/upload_xml/”$FilePath;
str_替换(“.php”、“.zip”、$filename);
$file\u content=file\u get\u contents($filename);//下载ZIP文件
打印(文件内容);
}
删除此行:
echo$file\u内容;

试试这个

$file_content = file_get_contents(dirname(__FILE__)."/uploaded_xml/".$FilePath);  // To download the ZIP file
header('Cache-Control: public');  
header('Content-Description: File Transfer');  
header('Content-type: application/force-download');
header('Content-Disposition: attachment;   filename="'.dirname(__FILE__)."/uploaded_xml/".$FilePath.'"');

已经删除了echo$file\u content;echo this dirname(file)。“/upload\u xml/”$FilePath.header('content-type:application/zip');如果我使用echo$file\u content,它正在下载.php扩展名文件,如果我将其重命名为.zip,它工作正常……您能告诉我将出现什么问题吗?替换您添加此header('Content-type:application/force download');要添加此标头('Content-type:application/zip');@user7334323,readfile($archive\u file\u name”);在标头之后替代print\r()/echo并重试一次。readfile($archive\u file\u name”);在标头之后替代print\r()/echo并重试一次。
$file_content = file_get_contents(dirname(__FILE__)."/uploaded_xml/".$FilePath);  // To download the ZIP file
header('Cache-Control: public');  
header('Content-Description: File Transfer');  
header('Content-type: application/force-download');
header('Content-Disposition: attachment;   filename="'.dirname(__FILE__)."/uploaded_xml/".$FilePath.'"');