Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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
Php 为什么我的zip文件无效或损坏(Windows无法打开该文件夹)?_Php_Zip - Fatal编程技术网

Php 为什么我的zip文件无效或损坏(Windows无法打开该文件夹)?

Php 为什么我的zip文件无效或损坏(Windows无法打开该文件夹)?,php,zip,Php,Zip,我试图用数据库中的某个文件制作一个zip,我在打包zip之前做了一些其他的事情,实际上我将文件读到另一台服务器,并将它们写在同一个文件夹中打包,但出于某种原因,我下载的zip文件无效 我确实看到了与这个问题相关的答案,但它们对我没有帮助。这是我的剧本: <?php include_once('..databaseconnection'); include_once('..databaseconnection'); function noticiaZip($idNoticia) {

我试图用数据库中的某个文件制作一个zip,我在打包zip之前做了一些其他的事情,实际上我将文件读到另一台服务器,并将它们写在同一个文件夹中打包,但出于某种原因,我下载的zip文件无效

我确实看到了与这个问题相关的答案,但它们对我没有帮助。这是我的剧本:

<?php 
include_once('..databaseconnection');
include_once('..databaseconnection');

function noticiaZip($idNoticia)
{
    //Utilizo el idTarea para traer las tareas del post correspondiente

    sql="Query->";

    $result = mysql_query($sql);
    $arrgArch = array();
    while ($row = mysql_fetch_array($result)) {
        $arrgArch[] = array('Archivo' =>$row['link'], 
                            'Nombre'=>utf8_encode($row['Nombre']),
                            'Titulo'=>utf8_encode($row['Titulo']) 
                            );
    }
    $zip = new ZipArchive();

    $filename = 'example.zip';

    if($zip->open($filename, ZipArchive::CREATE)===true) {
        foreach ($arrgArch as  $value) {

            //Divido el url para poder rescatar el nombre del archivo
            $nameFile = explode('/', $value['Archivo']);
            $count = count($nameFile);
            $name = $count - 1;

            //Url con el code para los espacios y que me acepte realizar el stream
            $url = str_replace($nameFile[$name], rawurlencode($nameFile[$name]), $value['Archivo']);
            $source =$url;

            $destination = $nameFile[$name];

            $data = file_get_contents($source);

            $handle = fopen($destination, "w+");
            $noticias = fopen("noticia.doc", "w+");

            fwrite($handle, $data);
            fclose($handle);

            $verNoticia=traeNoticiaN($idNoticia);

            foreach($verNoticia as $k => $v){
                fwrite($noticias, "Título:\n".$v['Titulo']."\n\n\n");
                fwrite($noticias, "Título:\n".$v['value']."\n\n\n");
                fwrite($noticias, "Título:\n".$v['value']."\n\n\n");
                fwrite($noticias, "Título:\n\n\n".$v['value']."\n\n\n");
                fwrite($noticias, "Título:\n\n\n".$v['value']."\n\n\n");
                fwrite($noticias, "Título:\n\n".$v['value']."\n\n\n");
                fwrite($noticias, "Título:\n\n".$v['value']."\n\n\n");
                fclose($noticias);
            }
            if($value!='noticiaZip.php') {
                $zip->addFile("temp/".$destination, $destination);
                $zip->addFile("temp/noticia.doc", "noticia.doc");
            }
        }
        $zip->close();
        foreach ($arrgArch as $value) {
            $nameFile = explode('/', $value['Archivo']);
            $count = count($nameFile);
            $name = $count - 1;
            $destination = $nameFile[$name];
            unlink($destination);
            unlink("noticia.doc");
        }          
    } // Set headers
    header("Content-type: application/zip");
    header("Content-disposition: attachment; filename=".basename($filename));
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public"); // Sin esto el IE no funciona
    header("Content-Transfer-Encoding: binary");
    header("Content-Length:".filesize($filename));

    ob_clean();
    readfile($filename);
    unlink($filename); 
    exit();
}
$noti = noticiaZip($_GET['idNoticia']);
?>


删除
取消链接($filename)
然后再试一次,我在某个地方读到过,一些人在强制下载脚本中使用此脚本时面临问题检查了下载的zip?是零字节吗?你能通过7z/wizip/winrar修复周期运行它并使它工作吗?在文本/十六进制编辑器中打开.zip以查看是否嵌入了任何可能导致问题的php警告消息?如果您只是说“其他答案对我没有帮助”,而没有进一步的详细信息,那么您就迫使我们可能在此处复制所有这些答案,只是为了将其丢弃。你能详细说明一下到目前为止你都做了些什么吗?很抱歉,我在这里搜索和阅读了一些帖子,认为我的问题是由标题行引起的,但到现在为止,没有找到答案。我对取消链接进行了评论,并保留了相同的问题,我尝试使用7z打开,但返回了一个错误,该文件无法作为存档打开。如@MarcB所说,脚本生成的zip文件大小是多少?并尝试用文本编辑器打开它!