16 January 2011

PHP - zipping remote files

I was thinking for a lot of time to do this... even if it has a huge negative part: network and CPU load. When you have a public album online, you should be able to download your original files from the server. If possible, via one big file - one archive.

The idea how to make it, came from here. Thank you Facebook!

I keep the original pictures and re-sized ones on different servers. When somebody would like to download one full album, he/she can click the download link on pictures4.net and watch how the archive is being created. At the end of the process, the archive link is displayed and the owner is allowed to download it... The original images are in the zip file, not the re-sized ones... videos are included as well!

Here is part of the code that i use to create the ZIP archive of remote files:


$zip = new ZipArchive();
if ($zip->open($zipName, ZipArchive::CREATE) === TRUE)
{
$query = 'select * from images';
$res=$sql->select($query);
$row = mysql_fetch_array($res);
if ($row['id'])
{
$_SESSION['zipAdded']++;
$zip->addFromString($row['name'],file_get_contents($webServer[mt_rand(0,count($webServer)-1)].$row['fileName']));
$zip->close();
echo $msg['wait_i_create_zip'].$row['name'].$msg['current_zip_size'].number_format((filesize($zipName)/1024/1024),2).' Mbytes

<img src="'.$webServer[mt_rand(0,count($webServer)-1)].$row['fileName'].'" alt="'.$row['name'].'" border="0">
</div>
</div>
<!-- refresh here -->';
}
else
{
echo '<div class="okMsg">
'.$msg['album_zipped_ok'].'<a href="zippedAlbums/'.str_replace($zipName).'">right click and save target as</a></div>
';
$_SESSION['zipDown']=1;

1 comment:

  1. sorry for the ugly indentation and missing div's... it seems that blogspot is removing them... the main idea is there anyway...

    ReplyDelete