0

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

Manoj Gupta
  • 63
  • 1
  • 1
  • 9
  • Take a look on http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-work-with-https – Michail M. Aug 12 '16 at 07:31
  • i think in the above url $image_url you printing the image with image tag but not the whole image. all you have to do is find the image regarding your query in the url and redirect the link to the image url then you can get the row image. – Dhaval Purohit Aug 12 '16 at 07:34
  • @dhaval Purohit Thanks for suggestion! but I am not printing with image tag. when you directly copy and paste the image url it will show image and i have to just download the that image in local system. – Manoj Gupta Aug 12 '16 at 07:41
  • ohk wait a minute i will give you the solution @ManojGupta – Dhaval Purohit Aug 12 '16 at 07:46

2 Answers2

5

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';
}
?>
Dhaval Purohit
  • 1,270
  • 10
  • 28
  • i have already created images folder in the same path where i have php file. if i use the this url `code` $imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png'; then it work but if i used `code` $imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU='; then its not working – Manoj Gupta Aug 12 '16 at 07:59
  • it is working in my localhost @ManojGupta the same code of yours if you are using the linux system then you have to first set the write permission in the images folder otherwise it is working with the windows. – Dhaval Purohit Aug 12 '16 at 08:15
  • I am using window 10 system. can it be some kind of authentication issue as its working the same code in your system and not in mine – Manoj Gupta Aug 12 '16 at 08:21
  • so tell me how can i see that what the actual problem is.? @ManojGupta – Dhaval Purohit Aug 13 '16 at 07:00
  • this saved me so much time and work thanks! – demo7up Jul 13 '21 at 15:57
1

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;
Dave
  • 3,073
  • 7
  • 20
  • 33
  • i tried to use copy function but it's shwoing error: `code` Warning: copy(https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=) [function.copy]: failed to open stream: No such file or directory – Manoj Gupta Aug 12 '16 at 07:51
  • @Manoj Gupta : change your folder name with "folder_name". replace images in the place of folder_name. – Dave Aug 12 '16 at 07:54
  • I have already created the images folder and change folder_name with 'images'. I think if i am using `code` $imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU='; it's not working . but if i using `code` $imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png'; then its working perfectly. – Manoj Gupta Aug 12 '16 at 08:04
  • @Manoj Gupta : its work perfectly for also your url https://cwsimages.ingramtest.com/cdsI..... – Dave Aug 12 '16 at 08:09
  • I understand it should work but unfortunatly its not working. – Manoj Gupta Aug 12 '16 at 08:16