PHP: Create and force download of an ISO image (resp. a zip etc...)

  toucheatout  2006-09-07 12:29  Web  

The base elements

This function supposes that a tree has been created on the filesystem and contains the entire contents of the CD (or zip) to be created and given to the user. The structure is as much as possible created using the symlink() function to save the overhead of copy. The temporary directory created is preferrably using an unique identifier, for instance using uniqid() function, giving something like $nameCD = uniqid('mycd_tmp_');.

mkisofs is needed on the server to create a suitable cdrom image. The CD will be created hybrid rockridge/Joliet with HFS extension, which bring on ore options:

Hiding OS-dependent files

The files AUTORUN.INF and all .exe are hidden to mac users. Same is done to hide macintosh-specific files (Start and index.macapp are given for example) to windows users.

$pretty_name is the name that will be proposed to the user when download begins.


function createAndDownloadISO($nameCD,$pretty_name){
exec('mkisofs -f -J -R -hfs -hide-hfs AUTORUN.INF -hide-hfs "*.exe" -hide-joliet Start -hide-joliet index.macapp -allow-leading-dots -o /tmp/'.$nameCD.'.iso /tmp/'.$nameCD);

header('Content-Type: application/force-download');
header('Content-Disposition: filename='.str_replace(" ","_",$pretty_name).'.iso;');
header('Content-Transfer-Encoding: binary');
header(Content-Length: '.filesize('/var/www/tmp/'.$nameCD.'.iso'));
readfile('/tmp/'.$nameCD.'.iso');
exec('rm -f /tmp/".$nameCD.".iso');
exec('rm -rf /tmp/'.$nameCD);
}

Adapting for alternative formats for download - e.g. zip

Creating on-the-fly zips to download works along the same line, just replace all the ".iso" by ".zip" and the mkisofs command by something approaching "cd /tmp/$nameCD ; zip -rq 0 ../nameCD.zip *". Think about the "dereference symlink" switch in case this is not the default behavior. (and if it could get quite lengthy to process and hit the php max execution time, try set_time_limit(0); - or any value that you see fit)

 
Informatics


yro.slashdot.org - Your Rights online


nytimes.com New York Times - International


Informatic headlines