Sorin Lascu
Sorin Lascu

Reputation: 405

Can't create archives with php zip library

I'm trying to develop for homework a MVC PHP app that archives some files, app which I'm building on a Zend Server in Windows 7 (if it matters).

Here's the setup : I have this form in a view :

<div class="box">
    <h3>What would you like to store?</h3>
    <form action="<?php echo URL; ?>archives/archiveAction" method="POST"  enctype='multipart/form-data'>
        <label>Select the files you'd like to store</label>
        <input type="file" name="upload[]" multiple>
        <input type="hidden" name="user_id" value="<?php echo $_SESSION['id']; ?>" >
        <label>Select your favorite compression algorithm</label>
        <select name="type_of_archive">
            <option value="zip">ZIP</option>
            <option value="tar">TAR</option>
            <option value="gzip">GZIP</option>
            <option value="bzip2">BZIP2</option>
        </select>
        <input type="submit" name="add_files" value="Submit" />
    </form>
</div>

And then I'm passing it to the responsible controller action:

public function archiveAction()
    {
        if(isset($_POST["user_id"]) && isset($_POST["type_of_archive"]) && isset($_FILES['upload']))
        {
            $time = new DateTime();
            if ($_POST['type_of_archive'] == 'zip')
            {
                Helper::zipFiles($_FILES['upload'],"./".$_SESSION['username']."/",$_SESSION['username']." - ".$time->format('Y-m-d H:i:s'));
            }
        }
    }

The zipFiles function itself is below :

static public function zipFiles($files, $dir, $name)
{
    $location = $dir.$filename.".zip";
    $zip = new ZipArchive();
    if ($zip->open($location, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
    }
    $num_of_files = count($files['name']);
    for($i = 0;$i<$num_of_files;$i++ )
    {
        $zip->addFile($files['tmp_name'][$i]);
    }
    $zip->close();
    return $location;

}

The problem is that I'm not getting any errors. What I'm trying to do is create a zip file in a directory. All I'm getting is that my browser is downloading an empty file called archiveAction. Anything I'm missing here? If I keep retrying the script just hangs after a while.

Upvotes: 0

Views: 147

Answers (1)

u_mulder
u_mulder

Reputation: 54841

This happens because you use $files['tmp_name'][$i]. If you print_r your $_FILES you will see that $files['tmp_name'][0] (for example) is just a file name without a path. So when you execute archiveAction script tries to find file $files['tmp_name'][$i] in a directory where it is (script) is run. Sure there's no such file, as it is stored in your /tmp or some other directory for temporary files.

So, you should either first copy your uploaded files (move_uploaded_file function) to some specific location, path to which you know, and create zip archive using these copied files and full path to them. For example, copy them to uploads folder of you site. And use:

$zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $somefile_name);

Or you can find out path to your temporary files folder (check php.ini) and use:

$zip->addFile($temporary_path . $files['tmp_name'][$i]);

Upvotes: 1

Related Questions