Search code examples
phpcompressionjpegarchive

When to add to archive?


I'm building a website that acts as a portfolio for a companies advertising works. They upload files, can view it in the gallery and such. Another feature they want is for privileged users to be able to download the entire gallery.

I am faced with two choices (that I can think of) add to the existing archive as the files are uploaded which will increase the response time or build the archive every time someone wishes to download the gallery.

I don't know the mechanics or data structures behind the zipping utility that php provides so I ask you which is the least expensive?

The images are all jpegs, so compression has no effect and there will be potentially hundreds of high resolution images.


Solution

  • A third choice: set up as a "cron" job to build the archive daily. This means that the process is not linked to user activity, is guaranteed to happen and will not hold users up.


    Edit: answering second part about which utility to use.

    The PHP manual gives a brief overview: http://php.net/manual/en/phar.fileformat.comparison.php - but there are basically two types or archive/compressions: - Zip, gzip, bzip (compression, but with ability to compress multiple files to one archive) - rar, tar (archiving to a single file, with ability to reduce size in building the archive)

    If you're not expecting to gain much by way of compression, then look at one of the archiving functions (tar/rar etc). The best known is "rar" but the licence does not allow PHP to create rar files, so you'll need to use "tar". Tar is the most common for Linux files. You can do this through PHP library called "phar" http://www.php.net/manual/en/intro.phar.php.

    If you think you might want to then squeeze something extra out, gzip is PROBABLY the best (but you should test to see). So take your tar and compress with gzip. Phar can also handle this for you (phar::compress) in one simple class.

    Final note: if you're on a Linux server, you may get the absolute best results by called tar and gzip commands (in that order) through exec(), as it's bread and butter stuff for Linux.