我須要將圖像從PHP URL保存到個人PC。 假設我有一個頁面, http://example.com/image.php
,只有一個「花」圖像,沒有別的。 如何使用新名稱(使用PHP)從URL保存此圖像? php
參見file()
PHP手冊 : 服務器
$url = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg'; $img = 'miki.png'; $file = file($url); $result = file_put_contents($img, $file)
我沒法使任何其餘解決方案起做用,但我可以使用wget: curl
$tempDir = '/download/file/here'; $finalDir = '/keep/file/here'; $imageUrl = 'http://www.example.com/image.jpg'; exec("cd $tempDir && wget --quiet $imageUrl"); if (!file_exists("$tempDir/image.jpg")) { throw new Exception('Failed while trying to download image'); } if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) { throw new Exception('Failed while trying to move image file from temp dir to final dir'); }
建立一個名爲images的文件夾,該文件夾位於您計劃放置要建立的php腳本的路徑中。 確保它具備每一個人的寫權限,不然腳本將沒法工做(它將沒法將文件上載到目錄中)。 ui
Vartec對 cURL 的回答對我不起做用。 因爲個人具體問題,它確實略有改善。 url
例如, spa
當服務器上有重定向時(例如當您嘗試保存Facebook我的資料圖像時),您將須要如下選項集: code
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
完整的解決方案變爲: get
$ch = curl_init('http://example.com/image.php'); $fp = fopen('/my/folder/flower.gif', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); fclose($fp);
$data = file_get_contents('http://example.com/image.php'); $img = imagecreatefromstring($data); imagepng($img, 'test.png');