I need PHP to zip certain files on my server

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
syllith
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 02, 2019 7:04 am

Mon Dec 02, 2019 7:53 am

Okay so here's the deal. I have a website that hosts a ton of files. I recently just implemented a "Download Manager" that helps you keep track of your files you want to download. Each file has a checkbox next to it that writes the download link to a text file and saves it to the server. So let's say I select 4 files. I'll now have a text file formatted like this:

https://mywebsite.com/files/something1.bin
https://mywebsite.com/files/something2.bin
https://mywebsite.com/files/something3.bin
https://mywebsite.com/files/something4.bin

In the download manager on my website, you can select each file 1 by 1 and download it, but I want to have the option for you to download them all as a single zip file. I can pretty easily take these links and convert them into a normal directory so they'll end up looking like this:

/root/media/Ares/webroot/mywebsite/files/something1.bin

That's not the issue. My issue is that once I have these links converted into a format PHP can use, I don't know how to actually make the zipping happen. First thing I tried was ziparchive. Despite my phpinfo() telling me that everything relating to zipping is enabled, this line of code kills the script and I get a 500 error:

$zip = new ZipArchive();

The fact that it dies after putting that tells me I probably don't actually have it enabled on my server. I looked and looked and couldn't find a good straight answer. Some people were saying I need PECL or something, but I'm not exactly sure what it is or how to use it. The info I'm finding is not very straightforward to me. Is there another built-in way I can zip these files? Otherwise, I was thinking I could use winrar or a similar program to zip the file with a shell_exec command, but that seems a little convoluted and I'm not sure how PHP would figure out that the zipping is complete and is ready for the next action. That's why I'd really hope for it to be something natively supported by PHP.

But yea, that's where I'm at here. I don't know a good, relatively easy way to go about doing this. Does anyone have a different suggestion, or can maybe help me get ziparchive to work? I'm on Linux Debian Stretch 9 running PHP-FPM 7.0

Thanks for any help you can provide.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 859
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 02, 2019 9:33 am

Have you checked your error log? the answer may be in there?

Have you tried displaying errors when testing? (only do this on a test machine)

It would be easier to help if you posted the code.
syllith
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 02, 2019 7:04 am

Mon Dec 02, 2019 10:28 am

Okay, so progress has been made. I installed php7.0-zip and it actually now works with the $zip = new ZipArchive; code. So from here I can probably reformat that txt file and add them. My next question is, do you know how PHP handles the progress? Let me elaborate. These files range anywhere from a few KB to several GBs. If someone selects 3 files and they total 3 gigs, do you know if PHPs ziparchive is capable of detecting when the zipping is complete so I can then make the download available to the client? I know to "save" the zip the last line is:

$zip->close();

So does that mean if I wrote something like this, how would it work?

$zip = new ZipArchive;
$zip->open('test_new.zip', ZipArchive::CREATE) === TRUE
$zip->addFile($filelocation1);
$zip->addFile($filelocation2);
$zip->addFile($filelocation3);
$zip->close();
echo "done";

Would it wait until the zipping is entirely finished before executing the echo "done";, or is it just going to run it as a "background process" and run it right away? I need it to wait for the process to be complete before telling the user it's ready.
chorn
php-forum GURU
php-forum GURU
Posts: 614
Joined: Fri Apr 01, 2016 2:18 am

Tue Dec 03, 2019 1:41 am

Haven't you tried the code?
syllith
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 02, 2019 7:04 am

Wed Dec 04, 2019 12:21 pm

I was at work when I wrote that, so I couldn't try it out. However, I spent the last few days writing it all out, and it works pretty much as perfectly. Except 1 weird thing. Some users were complaining that the zips where empty. When I checked, they were not. Found out they were on mobile, tried it, and sure enough, it said it was empty. Android and IOS both report it as empty (even though the file size suggests otherwise). I downloaded RAR for android, and it can read the files. Why would stock android/iphone zip readers not see any folders or files in the zip? Any ideas?
Post Reply