创建一个不创建ZIP的ZIP文件脚本(没有错误)


Create a ZIP file script not creating ZIPs (no errors)

我不是一个本地PHP开发人员,但我做了我能找到和hack在一起,所以请原谅我,如果这不是太有意义:

我有一个简单的(看起来如此)脚本,它在URL中接受两个参数。一个是简单的字符串(要创建的ZIP文件的标题),另一个是指向服务器上需要压缩的文件的音轨序列化数组。然后这些就可以正常传递并且不序列化等等。脚本如下:

<?php
$audioTitleVar = unserialize(rawurldecode($_GET['audioTitle']));
$audioArrayVar = unserialize(rawurldecode($_GET['audioArray']));
function create_zip( $files = array(), $destination = '', $overwrite = true ) {
    if(file_exists($destination) && !$overwrite) { return false; }
    $valid_files = array();
    if(is_array($files)) {
        foreach($files as $file) {
            if( file_exists($_SERVER['DOCUMENT_ROOT'] . str_replace("http://mydomain.com","", $file)) ) {
                $valid_files[] = $_SERVER['DOCUMENT_ROOT'] . str_replace("http://mydomain.com","", $file);
            }
        }
    }
    if(count($valid_files)) {
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        foreach( $valid_files as $file ) {
            $just_name = preg_replace("/(.*)'/?([^'/]+)/","$2",$file);
            $zip->addFile($file,$just_name);
            //$zip->addFile($file,$file); 
        }
        //echo '<p>The zip archive contains ' . $zip->numFiles . ' files with a status of ' . $zip->status . '</p>';        
        $zip->close();
        return file_exists($destination);
    } else {
        return false;
    }
}
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/jones/zip/' . $audioTitleVar . '.zip';
create_zip( $audioArrayVar, $fileName, true );
//echo '<p>File Path: ' . $fileName . '</p>';
var_dump(file_exists($fileName));
?>

我想我真正的问题在这里,是,虽然脚本没有错误…没有创建ZIP。随着时间的推移,我在函数的某些部分放置了输出,看看它是否达到了目标,它是-所以我被这个难住了。

我真正需要的是你们中的一个人扫描一下剧本,看看是否有什么明显的东西是行不通的!

可能是权限问题吗?PHP应该运行在CGI模式还是Apache?还是说这没什么区别?

这个脚本的骨架取自:http://davidwalsh.name/create-zip-php,但它从来没有工作过,即使只有那个版本。

PS -我只是想补充,如果我取消注释行,告诉我有多少文件在ZIP等,它似乎返回正确的信息…但是最后检查文件是否存在返回false。

好了,我终于把它修好了。

似乎在addFile()中有什么东西损坏了代码。

我改了这个:

foreach( $valid_files as $file ) {
            $just_name = preg_replace("/(.*)'/?([^'/]+)/","$2",$file);
            $zip->addFile($file,$just_name);
            //$zip->addFile($file,$file); 
        }

:

foreach( $valid_files as $file ) {
            // Use basename to get JUST the file name from the path.
            $zip->addFile($file, basename($file) ); 
        }

basename函数只获取要放在ZIP中的文件名。以前它是服务器上的整个路径,这导致Windows阻塞它。

希望有一天能帮到别人。