开发者

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

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-&g开发者_运维问答t;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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜