Manoj Gupta
Manoj Gupta

Reputation: 63

Save image from url to local System in php

Hi I am using the below code to download image from url but its not working when i am using this code

<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>

but if i changed the $imageUrl with this

$imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png';

it works, please suggest what is wrong in first url or can't we store image from https url

Upvotes: 0

Views: 11912

Answers (2)

Dave
Dave

Reputation: 3091

try copy

$imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png';
if (!file_exists('folder_name')) 
{
    mkdir('folder_name', 0777, true);
}
$img_path="folder_name/S2_pc.png";
copy($imageUrl , $img_path);
echo $img_path;

Upvotes: 1

Dhaval Purohit
Dhaval Purohit

Reputation: 1290

The problem is that the "images" you are using in the file_put_contents is not getting the path that where to save the file. try to make the images folder along with php file which having this code and see its working.

<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>

Upvotes: 5

Related Questions