试图保存图像,其名称完整的PHP

Hi i am using the script below to save an image FROM another domain into my own domain.

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

But i cant get this to work. the $path resulted in 123 but not 123.jpg(which i need). and am i doing the file get content and file put content right?

You can use basename() to determine the last part (= filename) of a path.

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

See the docs over at http://php.net/manual/en/function.basename.php

I would probably use http://php.net/parse_url.

And + is addition in PHP, not concatenation. Change it to a .

$path = 'thumbs/' . $thumbname;