Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在windows资源管理器中打开时PHP.zip文件下载错误_Php_Download_Zip_Windows Explorer_Winrar - Fatal编程技术网

在windows资源管理器中打开时PHP.zip文件下载错误

在windows资源管理器中打开时PHP.zip文件下载错误,php,download,zip,windows-explorer,winrar,Php,Download,Zip,Windows Explorer,Winrar,尝试在windows资源管理器中打开PHP创建的.zip文件时,我遇到了一个奇怪的错误 它在WinRar中运行良好 我正在创建的是一个SDS表单下载站点,您可以在其中选中所需的PDF文档类型,然后将其压缩 该过程运行良好,我在创建文件时没有错误。只有在使用windows资源管理器打开时才会发生错误 错误消息: Windows无法打开该文件夹。压缩(压缩)文件夹“文件名”无效。在Windows资源管理器中打开时出错 如果你想自己测试的话,这个网站是免费的 代码如下: <?php $e

尝试在windows资源管理器中打开PHP创建的.zip文件时,我遇到了一个奇怪的错误

它在WinRar中运行良好

我正在创建的是一个SDS表单下载站点,您可以在其中选中所需的PDF文档类型,然后将其压缩

该过程运行良好,我在创建文件时没有错误。只有在使用windows资源管理器打开时才会发生错误

错误消息: Windows无法打开该文件夹。压缩(压缩)文件夹“文件名”无效。在Windows资源管理器中打开时出错

如果你想自己测试的话,这个网站是免费的

代码如下:

<?php
    $error = "";        //error holder
    if(isset($_POST['createpdf'])){
        $post = $_POST;     
        $file_folder = "files/";    // folder to load files
        if(extension_loaded('zip')){    // Checking ZIP extension is available
            if(isset($post['files']) and count($post['files']) > 0){    // Checking files are selected
                $zip = new ZipArchive();            // Load zip library 
                $zip_name = time().".zip";          // Zip name
                if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
                    $error .=  "* Sorry ZIP creation failed at this time<br/>";
                }
                foreach($post['files'] as $file){               
                    $zip->addFile($file_folder.$file);          // Adding files into zip
                }
                $zip->close();
                if(file_exists($zip_name)){
                    // push to download the zip
                    header('Content-type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                    readfile($zip_name);
                    // remove zip file is exists in temp path
                    unlink($zip_name);
                }

            }else
                $error .= "* Please select file to zip <br/>";
        }else
            $error .= "* You dont have ZIP extension<br/>";
    }
?>

我将详细说明我的评论。您的ZIP文件包含有效的ZIP内容,并在结尾包含完整的HTML文档。我不是说您在结尾添加了正确压缩的HTML,它只是附加了:

无法从您共享的代码中说明这是如何发生的,但一旦您意识到这一点,就应该很明显地进行修复


教育猜测-我想你有这个:

if(file_exists($zip_name)){
    // push to download the zip
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
    readfile($zip_name);
    // remove zip file is exists in temp path
    unlink($zip_name);
}
?>
<!DOCTYPE html>
<head>
...
…或使用
if()
构造不运行它:

if(file_exists($zip_name)){
    // ...
}else{ ?>
    <!DOCTYPE html>
    <head>
<? }
if(文件存在($zip\u名称)){
// ...
}还有{?>
加

以前

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);

注意:添加此代码可在将zip内容发送到浏览器之前清除所有html

您已将网站的html附加到zip文件的末尾,不是吗?是的,如果有帮助的话,可以添加部分内容,这是一个很长的代码,因此不会将所有内容添加为大量重复的代码段。现在添加了html部分。可能是重复的完全正确,现在检查了记事本中的文件,您建议我如何解决此问题?问题是您添加了额外的输出。解决方法是不执行此操作。-
退出
为我执行了此操作
if(file_exists($zip_name)){
    // ...
}else{ ?>
    <!DOCTYPE html>
    <head>
<? }
ob_clean();
ob_end_flush();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);