2

I want to save images from url to my directory and its image name after downloaded into a csv.

I write the following code which store images. but its not working as I want. in below code $img is variable which fetch url from csv.

suppose I have 1000+ products in csv and every products have image url like www.example.com/images/image1.jpg

so I want to download this image to my local directory/server directory and store its image name after download to csv file so I can add only image name into database tables.

set_time_limit(1000);    
  while($url = $response->fetch()) {
      $my_image = file_get_contents($img);
      $my_file = fopen('/csvfiles/','w+'); 
      fwrite($my_file,$my_image);
      fclose($my_file);
    }

The some solution I found on other similar questions is

 $file = file_get_contents($img);
 file_put_contents("/csvfiles/Tmpfile.zip", fopen($img, 'r'));

But it is not applicable in my condition or not working.

Any help would be appreciated.

Edit New - As marked as duplicate I want to add some more info here -

As I have all products info in csv formats and while importing the products I also want to download images from its url. And it is not possible to download 1000+ product image manually. I checked the above solution but it is not applicable in this situation. As in above solution they download image from only particular page and rename it directly. so it is different scenario here.

New Code - I also tried this code still not working

$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
$fp = fopen("/csvfiles/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
Rahul
  • 763
  • 1
  • 12
  • 45
  • 1
    Possible duplicate of [Saving image from PHP URL](http://stackoverflow.com/questions/724391/saving-image-from-php-url) – Catalin MUNTEANU Feb 01 '16 at 11:03
  • Does csv file somehow really matter here? I thinks, that comment above should help. – Gino Pane Feb 01 '16 at 11:30
  • yes csv file is important .. As I have all products info in csv formats and while importing the products I also want to download images from its url. And it is not possible to download 1000+ product image manually. I checked the above solution but it is not applicable in this situation. As in above solution they download image from only particular page and rename it directly. so it is different scenario here. – Rahul Feb 01 '16 at 11:35

3 Answers3

0

Try with the following code.

set_time_limit(1000);
while ($url = $response->fetch()) {
    $my_image = file_get_contents($img);
    // check stricly that you have content of image
    if ($my_image !== false) {
        // download the image and store in the database.
        $my_file = fopen('/csvfiles/', 'w+');
        fwrite($my_file, $my_image);
        fclose($my_file);
    }
}
Alankar More
  • 1,107
  • 2
  • 8
  • 26
  • what is $response here ? or it should be $img which store image url from csv .. please confirm . – Rahul Feb 01 '16 at 11:14
  • the code will help you to ensure that you have an image from the URL if it is not then you decide what will be stored in CSV file instead of image file name say default name of file or noimage.jpg etc. – Alankar More Feb 01 '16 at 11:34
  • i tried this solution but getting fatal error PHP Fatal error: Call to a member function fetch() on a non-object Fatal error: Call to a member function fetch() on a non-object – Rahul Feb 01 '16 at 11:38
  • `$response` is undefined check the lines before`while` as `$response` is not null. it should be your record resource. – Alankar More Feb 01 '16 at 12:19
0

You need a filename as well, not only a directory:

// download the inage from the internet:
$my_image = file_get_contents($url);

// get filename from url:
$filename = basename($url);

$path = '/csvfiles/' . $filename;

// save image to that file:
$my_file = fopen($path,'w+');
fwrite($my_file, $my_image);
fclose($my_file);

// save $filename to DB
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Hi @Gavriel This works for me but I dont want to change images original names as it contains product info so is there ant way or alternative without using tempnam() and save images with original names . – Rahul Feb 02 '16 at 04:51
0

Hi below code works for me .. hope it will help someone..

Thanks @Gavriel and @Alankar

if(!empty($img)){
    $my_image = file_get_contents($img);
    file_get_contents(filename)

    $filename = $data[0] . ".jpg";
     echo $filename. "\n";
    if(!file_exists($filename))
        {
            $my_file = fopen($filename,'w+');
            file_put_contents($filename, $my_image);
            echo $filename . "\n";

        }
    }
    $image = "/uploads/" . $filename;
    echo $image . "\n";

Here $img store url of image. which is fetch from csv file.

Here data[0] is csv column which is used to store image name uniquely . so we can use file_exists(); otherwise whenever I run my script again and again it store images again and store it by random temp name.

Rahul
  • 763
  • 1
  • 12
  • 45