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

试图保存图像,其名称完整的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?

您好我正在使用下面的脚本将图像从另一个域保存到我自己的域中。 p> \ n

  //获取图像名称和扩展名
 $ img ='http://otherdomain.com/image/123.jpg'; 
 $ getname = explode('/',$ img  ); 
 $ thumbname = $ getname [count($ getname) -  1]; 
 //保存图像文件
 $ file = file_get_contents($ img); 
 $ path ='thumbs /'+ $ thumbname  ; 
 file_put_contents($ path,$ file); 
  code>  pre> 
 
 

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

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;