Rahul
Rahul

Reputation: 726

How to save images from list image url in csv

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);

Upvotes: 2

Views: 3299

Answers (3)

Rahul
Rahul

Reputation: 726

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.

Upvotes: 0

Gavriel
Gavriel

Reputation: 19237

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

Upvotes: 0

Alankar More
Alankar More

Reputation: 1115

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);
    }
}

Upvotes: 0

Related Questions