0

我之前访问过这篇文章,发现它很有用,但我想通过让它根据 URL 名称保存图像文件名来为其添加更多功能。

这就是我到目前为止所做的并且它有效。

$contents=file_get_contents('http://www.domain.com/logo.png');
$save_path="C:/xampp/htdocs/proj1/download/[logo.png]";
file_put_contents($save_path,$contents);

基本上,在我放置方括号的地方,我希望根据 URL 文件名获得动态。例如,如果我有这样的图像 url: https://cf.dropboxstatic.com/static/images/brand/glyph-vflK-Wlfk.png,我希望它将图像保存到具有确切图像名称的目录中,在这种情况下为glyph-vflK-Wlfk.png.

这可能吗?

4

3 回答 3

0

有一个功能,basename()

$url="http://www.domain.com/logo.png";
$contents=file_get_contents($url);
$save_path="C:/xampp/htdocs/proj1/download/".basename($url);
file_put_contents($save_path,$contents);

您可能想检查它是否已经存在file_exists()

于 2015-10-30T15:53:34.547 回答
0

据我了解,您正在尝试做的是以下内容:

$url = 'http://www.domain.com/logo.png';
$contents=file_get_contents($url);

$posSlash = strrpos($url,'/')+1);
$fileName = substr($url,$posSlash);

$save_path="C:/xampp/htdocs/proj1/download/".$fileName;
file_put_contents($save_path,$contents);
于 2015-10-30T15:48:39.577 回答
0

我会这样做

$url = "http://www.domain.com/logo.png";

$file = file_get_contents($url);
$path = "C:/xampp/htdocs/proj1/download/". basename($url);

return !file_exists($path) ? file_put_contents($path, $file) : false;
于 2015-10-30T16:02:23.517 回答