Welcome Guest, Not a member yet? Register   Sign In
Copying multiple images from a remote server
#1

[eluser]Unknown[/eluser]
This is my first code snippet therefore constructive criticism is very welcome!

I'm just starting to work with affiliate product feeds and I wanted product images to be served locally rather than being served by an external site. So, with the help of the cURL library, as created by Phil Sturgeon (https://github.com/philsturgeon/codeigniter-curl), I knocked up the following in 10 minutes to grab each image in a feed and store a local copy with the same filename.

Code:
function import_photos()
{
  $this->load->helper('file');
  
  $this->db->select('*');
  $this->db->from('tmp');
  
  $query = $this->db->get();

  
   if ($query->num_rows() > 0)
    {
     foreach($query->result() as $row)
     {
      $results =  $row->photo;
      $img = $this->curl->simple_get($results);
      $filename = basename($results);
      
      // The path is relative to your main site index.php file
      if ( ! write_file("./images/incomming/" . $filename, $img))
      {
       echo 'Unable to write file: ' . $filename;
      }      
     }
    }
   else
   {
    echo 'No photos found';
   }
}

The code assumes that you have a table called tmp with a field called photo that contains the urls of each image. The images directory must be writeable and the cURL library is autoloaded in autoload.php


Messages In This Thread
Copying multiple images from a remote server - by El Forum - 10-29-2011, 08:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB