简体   繁体   English

尽管没有错误,也没有创建php zip文件,没有权限问题,没有空文件夹,没有

[英]php zip file not created despite no erros, no permission problems, no empty folders, no

Okay. 好的。 Every file exists, is readable, and is writable. 每个文件都存在,可读且可写。 ZIP produces no errors, and outputs: "numfiles: 140 status:0". ZIP不会产生任何错误,并输出:“ numfiles:140 status:0”。

the code reads a log, checks for specific text, then imports a number of images into a zip folder. 该代码读取日志,检查特定文本,然后将许多图像导入zip文件夹。 everything runs great except the zip folder is always empty. 除了zip文件夹始终为空之外,其他所有程序都运行良好。 I've read a lot of threads about this, and they all were resolved by changing permissions, modifying paths and checking for read/write/exist/errors. 我已经阅读了很多关于此的主题,并且所有这些主题都通过更改权限,修改路径并检查读/写/存在/错误来解决。 but... nothing has worked. 但是...没有任何效果。 whats up? 这是怎么回事?

<?php
$file = fopen("log.log", "r") or exit("Unable to open file!");

$zip = new ZipArchive();
$filename = "E:/Web Sites/whatever/order_stream/images.zip";
$try_file = $zip->open($filename,ZIPARCHIVE::OVERWRITE);

if ($try_file !== true) {
    exit("cannot open <$filename>\n");
}

while(!feof($file)) {
    $line = fgets($file);
    $results = explode(": ", $line);
    if ($results[0] == "Copying" || $results[0] == "File already exists, overwriting") {
        $file_name = substr($results[1],19);
        $to_zip = "E:/Web Sites/whatever/catalog/pictures/".$file_name;
        $to_zip = trim($to_zip);
        if (file_exists($to_zip)) {
            $zip->addFile($to_zip);
        }
    }
}

echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";

$zip->close();
fclose($file);
?> 

The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. ZipArchive :: addFile()方法接受文件的路径作为它的第一个参数,但是并非所有路径都相同。 addFile() method silently rejects (bug?) the file and you never know what went wrong. addFile()方法以静默方式拒绝(错误?)文件,而您永远不知道出了什么问题。 An alternative approach would be: 一种替代方法是:

// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);

In addition to getting the code working, file_get_contents() also generates decent error messages if you made an error in the path. 除了使代码正常工作之外,如果您在路径中出错,file_get_contents()还会生成不错的错误消息。 In this example 在这个例子中

$file = $full_directory_path_ending_with_slash.$filename;

Since in the above question, you have the parts in your fingers, the code could simply become: 由于在上面的问题中,您已经掌握了各个部分,因此代码可以简单地变成:

$content = file_get_contents($to_zip);
$zip->addFromString($filename, $content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM