Daniel Lematy
Daniel Lematy

Reputation: 167

php zip archive is adding the files with no size

I got this working but when i look into the zip folder all the files size is 0. I tryed not adding the files to the zip and that worked, but when i try to add them to a zip the size goes to 0. Why is this.

here is my php

if(isset($_FILES['file'])){

    $file_folder = "uploads/";

    $zip = new ZipArchive();
    $zip_name = time().".zip";
    $open = $zip->open("zip/".$zip_name, ZipArchive::CREATE);

    if($open === true){
        for($i = 0; $i < count($_FILES['file']['name']); $i++)
        {
        $filename = $_FILES['file']['name'][$i];
        $tmpname = $_FILES['file']['tmp_name'][$i];

        move_uploaded_file($tmpname, "uploads/".$filename);

        $zip->addFile($file_folder, $filename);
        }
    $zip->close();

    if(file_exists("zip/".$zip_name)){
    // zip is in there, delete the temp files
    echo "Works";
    for($i = 0; $i < count($_FILES['file']['name']); $i++)
    {
        $filenameu = $_FILES['file']['name'][$i];
        unlink("uploads/".$filenameu);
    }
    } else {
        // zip not created, give error
        echo "something went wrong, try again";
    }

    }
}

Upvotes: 2

Views: 1490

Answers (1)

ɴᴀᴛᴇ ᴅᴇᴠ
ɴᴀᴛᴇ ᴅᴇᴠ

Reputation: 4621

Your problem lies with this line: $zip->addFile($file_folder, $filename);

Currently that passes a path to the /uploads/ directory as the first argument.

According to Zip::addFile documentation you should be passing the path to the file to add (this includes the file and extension).

So change your code to include the file name (you already have it as a variable $filename which is handy).

$zip->addFile($file_folder.$filename, $filename);

Upvotes: 1

Related Questions