2012-04-14 81 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