2012-04-14 35 views
0

嗨,我使用下面的腳本將圖像從另一個域保存到我自己的域。試圖保存圖像的名稱完好無損php

//get image name and extension 
    $img = 'http://otherdomain.com/image/123.jpg'; 
    $getname = explode('/', $img); 
    $thumbname = $getname[count($getname) - 1]; 
    //save the image file 
    $file = file_get_contents($img); 
    $path = 'thumbs/'+ $thumbname; 
    file_put_contents($path, $file); 

,但我不能得到這個工作。 $路徑導致123但不是123.jpg(我需要)。 和我在做文件獲取內容和文件把內容正確嗎?

+0

也許您使用帶有「隱藏已知文件擴展名」選項的窗口。你的腳本對我完全沒問題。 – 2012-04-14 08:36:55

回答

2

您可以使用basename()來確定路徑的最後部分(=文件名)。

$image_name = basename($img); // This will contain 123.jpg 

請參閱該文檔了在http://php.net/manual/en/function.basename.php

+0

雖然很好的建議,但他的腳本不是什麼'錯誤'。他的爆炸將在技術上正確提供給定的URL的文件名。問題是他試圖用+作爲連接。 – Corbin 2012-04-14 08:40:03

2

我可能會使用http://php.net/parse_url

+是PHP中的附加內容,而不是串聯。將其更改爲.

$path = 'thumbs/' . $thumbname; 
+0

哦,謝謝你的建議。 – sm21guy 2012-04-14 08:37:59