创建 ZIP 文件脚本而不创建 ZIP(无错误)

发布于 2024-12-08 21:01:39 字数 2188 浏览 0 评论 0原文

我不是本地 PHP 开发人员,但我会用我能找到的东西来凑合,所以如果这没有多大意义,请原谅我:

我有一个简单的(看起来如此)脚本,需要两个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。

I'm not a native PHP developer but I make do with what I can find and hack together out there, so please excuse me if this doesn't make too much sense:

I have a simple (so it seems) script that takes two arguments in the URL. One is a simple string (title of the ZIP to be created) and the other is a serialized array of audio tracks that point to the files on the server that need zipping. These then pass just fine and are unserialized etc. Here's the script:

<?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));

?>

I guess my real issue here, is that, although the script DOES NOT error...no ZIP is created. I have, over time placed outputs in the function at certain parts to see if it was even getting there, and it is - so I'm stumped on this.

What I really need is for one of you guys to scan the script over to see if there's anything glaringly obvious that just won't work!

Could it be a permissions thing? Should PHP be running in CGI mode or Apache? Or does this not make a difference?

The bones of this script were taken from: http://davidwalsh.name/create-zip-php but it's never worked even with that version alone.

PS - I'd just like to add, that if I uncomment the line that tells me how many files are in the ZIP etc, it seems to return the correct info...but the final check on whether the file exists returns false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-12-15 21:01:39

好吧,我终于成功了。

似乎 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 被它阻塞。

希望有一天这可以帮助别人。

Ok, I have finally got it working.

Seems there was something in the addFile() that corrupted the code.

I changed this:

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

To this:

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

The basename function gets only the file NAME to then place in the ZIP. Before it was the whole path on the server, and this was causing Windows to choke on it.

Hope this helps someone someday.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文