encrypted21
encrypted21

Reputation: 194

PHP ZipArchive doesn't create a zip archive. What to fix in the code?

I have an .srt file located in the files/srt/username/filename.srt directory. I need to be able to download it in the browser, but to make this possible I have to zip the file first. I found the following code online:

function download_zip() {
    if (isset($_POST['download_srt'])) {
        $zip = new ZipArchive();
        $zip->open("files/srt/".$_SESSION['user_name']."/".$_POST['download_srt'].".zip", ZipArchive::CREATE);
        // output: files/srt/username/filename.srt.zip

        $zip->addFile($_POST['download_srt']);
        // output of $_POST['download_srt']: filename.srt

        $zip->close();
    }
}

The code is called when a submit button is pressed and the $_POST data are sent. The function works, but no ZIP file gets created in the same directory as the original srt file. No error messages appear.

Upvotes: 0

Views: 104

Answers (1)

Kevin Bui
Kevin Bui

Reputation: 524

You should provide the correct path to the file to add:

$zip->addFile("files/srt/".$_SESSION['user_name']."/".$_POST['download_srt']);

Upvotes: 1

Related Questions