如何从url将图像保存到我的目录中?

时间:2015-10-31 14:09:13

标签: php fopen fwrite

这是我的代码,我试图在我的目录中保存图像,图像来自stamps.com服务器,这是运输标签。

问题在于图片网址,那么如何从此图片网址中保存我的目录中的图片?请建议我?

<?php

$image_link = "https://swsim.stamps.com/Label/label.ashx/label-200.png?AQAAAKAUuNCk4tIIR-Wcnzg_AVv9iN6G78nuM772Yl14nLVR3ysEURQ-d1nDKj8i5W0QD0rdOzM7ZuZtbMvsYmhn2SgxfmT9mJ3sLuKFFxIvHj14QEr-AY9Sm7zw4sWzP0B5Q8p1Z23Spjz5OnVO95zvu_d8F89oUNOkHa9oD5vhA-rOt09WGjULw6tn5x1a3149veMS12NVWBYsjLEqBRVJImWmm3bsxRbGrYu6STuV4vmYnbFT9po9azuUlsei_bzlLq7UBiVVZr1scjq97PAJew0o5fRUds6dSvpCup8pioQG8qlDkRVSwkSruiKWoffxZnhoUDfDlAIX0-NGQh8BVupmPDIQMr7ZpexhGIBykih4QREg-GrBLfyED9C4HzE58PapsPaBN9nxKwcv6yIKLaHCXCmbBEUGIIJI0jfvjRenO1PXrU1H29GNp7I9n6y-HQY--jXByPpyonOyddn9PIEgL-DPrGbmnAZVwpgQoqhqUBYllll0ErgaVbYf78sLeQf-QqCEyJ0ACP4FCKGCtudhsNisouEfdVue3JzblQScNzq3-_vC1UPWoMXHY3qoN2L28C0eDcD7Y1oMqAAkfN1E4ROAypAZ";//Direct link to image
$split_image = pathinfo($image_link);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $image_link);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response= curl_exec ($ch);
curl_close($ch);
$file_name = "images-folder/".$split_image['filename'].".".$split_image['extension'];
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);

我得到的错误:

Warning: fopen(images-folder/label-200.png?AQAAAKAUuNCk4tIIR-Wcnzg_AVv9iN6G78nuM772Yl14nLVR3ysEURQ-d1nDKj8i5W0QD0rdOzM7ZuZtbMvsYmhn2SgxfmT9mJ3sLuKFFxIvHj14QEr-AY9Sm7zw4sWzP0B5Q8p1Z23Spjz5OnVO95zvu_d8F89oUNOkHa9oD5vhA-rOt09WGjULw6tn5x1a3149veMS12NVWBYsjLEqBRVJImWmm3bsxRbGrYu6STuV4vmYnbFT9po9azuUlsei_bzlLq7UBiVVZr1scjq97PAJew0o5fRUds6dSvpCup8pioQG8qlDkRVSwkSruiKWoffxZnhoUDfDlAIX0-NGQh8BVupmPDIQMr7ZpexhGIBykih4QREg-GrBLfyED9C4HzE58PapsPaBN9nxKwcv6yIKLaHCXCmbBEUGIIJI0jfvjRenO1PXrU1H29GNp7I9n6y-HQY--jXByPpyonOyddn9PIEgL-DPrGbmnAZVwpgQoqhqUBYllll0ErgaVbYf78sLeQf-QqCEyJ0ACP4FCKGCtudhsNisouEfdVue3JzblQScNzq3-_vC1UPWoMXHY3qoN2L28C0eDcD7Y1oMqAAkfN1E4ROAypAZ): failed to open stream: Invalid argument in C:\xampp\htdocs\upwork\test.php on line 13
X_x

1 个答案:

答案 0 :(得分:-1)

<?php $url = 'http://example.com/image.php'; $img = '/my/folder/flower.gif'; file_put_contents($img, file_get_contents($url));