is unarchiving via ZipArchive faster than using exec() in php? - php

I am writing an app that will need to unzip user uploaded archives. PHP provides the ZipArchive class, but it should also be possible to unzip using unzip via exec(), my question is which is preferrable in terms of performance & scaling?

A you use php, I guess it would be an online App.
As Brian hinted library will most probably be faster as it has much less to do.
On the other hand if you really want to know for sure, just try. Things like compilation options for ZipArchive and binary unzip can have high impact.
You should also consider unziping as a background task with some ajax to warn when it's done as unzipping can be a long task.

Related

Cannot zip more than 4GB, cannot tar with filenames > 100 characters, what's next?

I am trying to rebuild a backup system that currently zips boatloads of files for easy archiving (using PHP's ZipArchive class). The problem I'm having is that ZipArchive only supports files up to 4GB.
I thought I found a solution: PharData
But the PharData class comes with its own problems, namely, filenames cannot be greater than 100 characters for tar archives.
I've read about solutions for command line users (Zip64 and extended headers for tar files), but I don't see solutions built-in or that are native to PHP.
Am I missing the right tool for the job? Or is this a common roadblock?
Thank you!
(I'm horrible at making question titles, sorry!)
Edit: The files I am backing up are stored on the server with a random string of text and the real filename is repopulate when the files are zipped. I'm concerned about passing user submitted filenames to my Linux command line.
PharData is meant to be used as part of the Phar PHP application archiving format. It's not intended for general use.
Use the Archive_Tar PEAR class to create tar archives.
You can use shell scripting, create bash script with couple of shell commands to make .tar of all the files using Linux built in command.
This link might be useful to read further
http://broexperts.com/2012/06/how-to-backup-files-and-directories-in-linux-using-tar-cron-jobs/

Self executable PHP files

I'm writing some scripts in PHP for mangling data etc and I would like to be able to hand these over to our client to run without all the overhead of them having to install PHP first.
I was wondering if there's a way to bundle PHP itself with a script to create an exe file? This sounds like I might have to compile php in a clever way of some kind and get it to run itself with the file argument when it's executed.
Is there something out there to do this? It'd be really handy! I understand you'd end up with a fairly big executable because of the overhead of having the php executable bundled within it, but I think the benefits outweigh the down sides.
Any other suggestions?
Thanks,
John.

Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?

I need to do the following image manipulations for images uploaded by users on my site:
Resize images (if greater than a certain dimension)
Convert all image formats to jpg's
Add a watermark to the bottom of all images
Do I need to use either the MagickWand or iMagick extensions or can I just get away with running the terminal commands inside PHP's exec function?
Is there a reason why the PHP extensions would be preferred? Which would be faster and better for performance (this site may have lots of users and there would be a ton of image processing at any given time)?
I'd like to make a counterpoint to drew010's answer. In his answer he states:
You would benefit a lot using the PHP extensions instead of using exec
or similar functions. Built in extensions will be faster and use less
memory as you will not have to spawn new processes and read the output
back
For processing images, this is true. Well, I know for a fact that calling on ImageMagick binaries using PHP's exec() function (or similar functions) carries additional overhead above using embedded PHP libraries however I'm not clear on how much overhead there is.
But there's another side of the coin. If you embed the ImageMagick code into PHP via an extension, then every request using PHP takes more memory whether the request processes an image or not. If you're using Apache's mod_php then this becomes even more of an issue because now every request to your server has the ImageMagick libraries in memory even if it's serving an HTML file!
So it really depends. You really want to keep PHP's memory footprint as low as possible. If you're processing a large number of requests which require ImageMagic and you're using FastCGI or php_fpm, then you'll probably see a benefit to using embedded ImageMagick. But if you're only occasionally processing requests using ImageMagick and/or using Apache's mod_php then you may get much better performance calling ImageMagick via exec().
You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.
If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.
I always use PHP GD http://php.net/manual/en/book.image.php
You can accomplish resizing, converting to JPG and watermarking your images. I know your post said you have to use MagickWand or iMagick, but I just wanted to present this option in case it would work for you.

Which is More Efficient: Zipping With a System Command or Using PHP ZipArchive?

I've recently been given a task that involves uploading a zip file, storing it in a database as a blog, and then extracting and presenting the contents of that zip file when the client requests it.
I've got two approaches for this task: Using the exec command to execute the zip command native to the Linux OS the web server is running on, or using the ZipArchive class that comes with PHP.
Which approach uses the least amount of memory?
Which approach offers the most flexibility? W
What are the major advantages of one approach
over the other?
exec('zip') is way faster for large/many files. The built-in routines are always slower(due to many library calls & overhead. The system zip may have the advantage of using highly optimized routines. As a plus to the exec method, is the ease to change output formats from zip to rar, or 7zip or bzip etc...

Libraries other than ZipArchive for creating pkzip archives in PHP?

I'm in the process of writing some epub creation functionality using php5. Currently I am attempting to use ZipArchive but have run into a couple annoyances with it. First of all, there is no functionality to set the compression level. Second of all, ZipArchive::addFile() seems to fail silently and create a corrupt archive whenever I use it. I have been using file_get_contents() + ZipArchive::addFromString() instead but would prefer to just use the documented function for adding files.
I will not post code samples unless someone would really like to help me debug this issue, but rather I'm wondering if there are any other libraries for creating zip (pkzip) archives in PHP that you would recommend. So far, I have seen PclZip, whose site does not seem to be loading, but not much else. I have also considered using exec() + zip (unix command). This code will only run on this one particular linux box so portability is not an issue.
Thanks in advance for any suggestions!
PCLZip is pretty good alternative, with zlib as its only dependency, if you can get access to the site. It's probably temporary, it was certainly accessible between Christmas and New Year.
It's also pretty efficient, even in comparison with ZipArchive
EDIT
You say that you've had problems with ZipArchive's addFile() method. Is this in a Windows environment, or on your Linux server? I know that there have been a few buggy releases of the php_zip library on Win32 that can give this problem, although the latest versions seem OK, and I've not encountered the same problem on other platforms (even the WIN64 version).
I'd use exec() and the Unix command. A native-to-the-system way to solve the problem - the unix utils will always be a step or two ahead from their PEAR counterparts.

Categories