Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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 Ziparchive排除文件夹&;如何使其通用_Php_Zip_Ziparchive - Fatal编程技术网

Php Ziparchive排除文件夹&;如何使其通用

Php Ziparchive排除文件夹&;如何使其通用,php,zip,ziparchive,Php,Zip,Ziparchive,这是我在互联网上找到的一段代码,用于创建整个文件夹的.zip文件并下载 我想做一些改变,但我尝试的一切都不起作用。我想要的是: 使其通用化(即,我希望将makezip.php放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如www.domain.com/files/media/images/它都会得到该文件夹) 下载后删除zip(我认为我做得很好,使用comand取消链接,但我不确定这是正确的方法 删除zip也会获得的和。文件夹 导出名为date.hour.director

这是我在互联网上找到的一段代码,用于创建整个文件夹的.zip文件并下载

我想做一些改变,但我尝试的一切都不起作用。我想要的是:

  • 使其通用化(即,我希望将
    makezip.php
    放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如
    www.domain.com/files/media/images/
    它都会得到该文件夹)

  • 下载后删除zip(我认为我做得很好,使用comand
    取消链接,但我不确定这是正确的方法

  • 删除zip也会获得的
    文件夹

  • 导出名为
    date.hour.directory.zip
    (ex
    18Sep2013\u 13-26-02\u images.zip
    )的zip。我尝试过这样做
    $fd=getcwd();
    $filename=date(“dMY\u H:i:s”).“'.$fd..zip”;
    但它不起作用,它只得到$fd变量

我知道它不应该给你所有的代码,也不希望你看到它,也不希望你调试它,但我已经尝试了所有的方法,但都不管用。我正在自学,我对php有点生疏

致意

<?php
        function download($file) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($file));
                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($file));
                ob_clean();
                flush();
                readfile($file);
        unlink($file);
                exit;
        }

        $directory = '.';
        $files = scandir($directory);
        if(empty($files)) 
        {
                echo("You haven't selected any file to download.");
        } 
        else 
        {
                $zip = new ZipArchive();
                $filename =  date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
                if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
                                exit("Cannot open <$filename>\n");
                }
                $N = count($files);
                for($i=0; $i < $N; $i++)
                {
                  $zip->addFile($files[$i], $files[$i]); //add files to archive
                }
                $numFiles = $zip->numFiles;
                $zip->close();

                $time = 8; //how long in seconds do we wait for files to be archived.
                $found = false;
                for($i=0; $i<$time; $i++){

                 if($numFiles == $N){   // check if number of files in zip archive equals number of checked files
                        download($filename);
                        $found = true;
                        break;
                 }
                 sleep(1); // if not found wait one second before continue looping
         }

         if($found) { }
                 else echo "Sorry, this is taking too long";
         }
?>

好的,我已经自己解决了我的问题,我正在为那些想要的人分享解决方案

  • 使其通用(即,我希望将makezip.php放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如www.domain.com/files/media/images/它获取该文件夹)
我修改了index.php上的代码,并对zip.php(处理压缩过程的文件.Simples solution..Dumb)做了一些更改

1-index.php-只需放置一个简单的链接

<a href="zip.php?dirz=HERE_GOES_THE_FOLDER_URL">Download folder as a .zip</a>

  • 下载后删除zip(我想我做得很好,使用comand Unlink,但我不确定这是正确的方法
是的,它是正确的。(在zip.php上)

取消链接($ZIPNAME);


  • 删除zip也会得到的.and..文件夹
$direct=array_diff(scandir($directory),array(“.”,“.”,“error_log”);


  • 导出名为date.hour.directory.zip(例如18Sep2013_13-26-02_images.zip)的zip文件。我尝试了这样做$fd=getcwd();$filename=date(“dMY_H:I:s”)。“'$fd..zip';但它没有工作,它只获取$fd变量
简单到:

$zipname = date("dMY_H-i-s").'_'.basename($directory).'.zip';

最好的祝愿

使用
for
循环并将
sleep(1)
放入循环中,停止下载文件不会有任何区别,它将继续输出文件内容直到结束。您只能使用
设置时间限制(8*60)
设置运行脚本的最大执行时间(但这并不意味着它将在启动8秒后停止,它不计算其他进程获得处理器时间时在内存中花费的空闲时间,也不计算脚本进行的db或系统调用)谢谢Ivan。正如我所说,我对这一点不熟悉。现在寻找其他答案
<?php
        function download($file) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($file));
                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($file));
                ob_clean();
                flush();
                readfile($file);
        unlink($file);
                exit;
        }

        $directory = '.';
        $files = scandir($directory);
        if(empty($files)) 
        {
                echo("You haven't selected any file to download.");
        } 
        else 
        {
                $zip = new ZipArchive();
                $filename =  date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
                if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
                                exit("Cannot open <$filename>\n");
                }
                $N = count($files);
                for($i=0; $i < $N; $i++)
                {
                  $zip->addFile($files[$i], $files[$i]); //add files to archive
                }
                $numFiles = $zip->numFiles;
                $zip->close();

                $time = 8; //how long in seconds do we wait for files to be archived.
                $found = false;
                for($i=0; $i<$time; $i++){

                 if($numFiles == $N){   // check if number of files in zip archive equals number of checked files
                        download($filename);
                        $found = true;
                        break;
                 }
                 sleep(1); // if not found wait one second before continue looping
         }

         if($found) { }
                 else echo "Sorry, this is taking too long";
         }
?>
$zipname = date("dMY_H-i-s").'_'.basename($directory).'.zip';