Add picture from URL to zip archive in php

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 223
Location: Holland Rotterdam

Add picture from URL to zip archive in php

Hello chief hope you do fine ?

I have here a code, it create a zip-archive + place a text--document in the zip-archive
this works correctly.... But I would like to add a picture in the zip-archive
I tried some things but nothing is working

Code: Select all

<?php	
$today = date("F j, Y, g:i a");  

$files ='http://145.53.93.209/Molendijk/add/load/pictures/'.$users["post_image"].'';

$zip = new ZipArchive;
if ($zip->open(''.$users["post_user_name"].'/Backup/'.$users["title"].'.zip', ZipArchive::CREATE)  == TRUE){
    // Add files to the zip file
    $zip->addFile(''.$users["post_user_name"].'/Backup/'.$users["title"].'');
   
    $zip->addFile(''.$users["post_user_name"].'/Backup/'.$users["title"].'/$files');
 
    // Add File3.txt file to zip and rename it to File5.txt
 
    // Add a file newFile.txt file to zip by adding specified text in newFile.txt
    $zip->addFromString('@Molendijk-'.$users["title"].'.txt', 'Welcome to @Molendijk I just extracted your '.$users["title"].' 
on '.date("F j, Y, g:i a").'  
your located with ip: '.$_SERVER["REMOTE_ADDR"].' 

'.$users["post_text"].'

');
 
    // All files are added, so close the zip file.
    $zip->close();
}
?>

I try with $files to add the picture into the zip-archive

Code: Select all

$files ='http://145.53.93.209/Molendijk/add/load/pictures/'.$users["post_image"].'';

Code: Select all

$zip->addFile(''.$users["post_user_name"].'/Backup/'.$users["title"].'/$files');
Please help me out....

Admin
Hello,
I'm fine.

To add a file from an url address into a zip archive, use this code:

Code: Select all

$url ='http://domain.com/address/file_name';

$zip = new ZipArchive();
if($zip->open('dir/file.zip', ZipArchive::CREATE)  == TRUE){
  #add file from $url into the zip
  $get_file = file_get_contents($url);
  $zip->addFromString(basename($url), $get_file);

  $zip->close();
}
So, in your script you can use this code:

Code: Select all

<?php	
$today = date("F j, Y, g:i a");  

$url ='http://145.53.93.209/Molendijk/add/load/pictures/'.$users["post_image"].'';

$zip = new ZipArchive;
if($zip->open(''.$users["post_user_name"].'/Backup/'.$users["title"].'.zip', ZipArchive::CREATE)  == TRUE){
  #add file from $url itto the zip
  $get_file = file_get_contents($url);
  $zip->addFromString(basename($url), $get_file);
   
  // Add a file newFile.txt file to zip by adding specified text in newFile.txt
  $zip->addFromString('@Molendijk-'.$users["title"].'.txt', 'Welcome to @Molendijk I just extracted your '.$users["title"].' 
on '.$today.'  
your located with ip: '.$_SERVER["REMOTE_ADDR"].' 

'.$users['post_text']);

  // All files are added, so close the zip file.
  $zip->close();
}
?>

JanMolendijk
Thanks for the great support, all my content with
comments + pictures are now archived in .zip-map.

I also adding partners on my website &nd you're the second partner.

Thanks again

Similar Topics