0

嗨,我正在使用下面的脚本将来自另一个域的图像保存到我自己的域中。

  //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);

但我不能让它工作。$path 导致123但不是123.jpg(我需要)。我在做文件获取内容和文件放置内容吗?

4

2 回答 2

2

您可以使用basename()来确定路径的最后一部分(= 文件名)。

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

请参阅http://php.net/manual/en/function.basename.php上的文档

于 2012-04-14T08:36:00.723 回答
2

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

并且+是 PHP 中的加法,而不是串联。将其更改为.

$path = 'thumbs/' . $thumbname;
于 2012-04-14T08:36:13.167 回答