2013-07-21 3 views
4

이미지를 업로드 할 때 확인을 누릅니다. 보통 다음과 같은 작업을 수행합니다.Laravel 4 url에서 이미지 가져 오기

$file = Input::file('image'); 
$destinationPath = 'whereEver'; 
$filename = $file->getClientOriginalName(); 
$uploadSuccess = Input::file('image')->move($destinationPath, $filename); 

if($uploadSuccess) { 
    // save the url 
} 

사용자가 이미지를 업로드 할 때 제대로 작동합니다. 하지만 URL에서 이미지를 저장하려면 어떻게해야합니까 ??? 다음

$url = 'http://www.whereEver.com/some/image'; 
$file = file_get_contents($url); 

과 :

내가 좋아하는 뭔가를하려고하면

$filename = $file->getClientOriginalName(); 
$uploadSuccess = Input::file('image')->move($destinationPath, $filename); 

내가받을 다음과 같은 오류 :

그래서
Call to a member function move() on a non-object 

, 어떻게이에서 이미지를 업로드 할 laravel 4가있는 URL?

에이미가 크게 감사드립니다.

답변

1

Laravel의 Input :: file 메서드는 POST 요청으로 파일을 업로드 할 때만 사용됩니다. file_get_contents가 laravel의 클래스를 반환하지 않기 때문에 오류가 발생합니다. url에서 가져온 파일은 tmp 폴더에 업로드되지 않기 때문에 move() 메서드를 사용할 필요가 없습니다.

대신 여기에 설명 된 내용을 PHP upload an image file through url 사용해야합니다.

등 :

// Your file 
$file = 'http://....'; 

// Open the file to get existing content 
$data = file_get_contents($file); 

// New file 
$new = '/var/www/uploads/'; 

// Write the contents back to a new file 
file_put_contents($new, $data); 

지금은 그것을 확인하지 수 있지만 나쁜 솔루션처럼 보인다. 그냥 URL에서 데이터를 얻을 당신이 당신의/공공/IMG에 이미지를 저장합니다

+1

오히려 디렉토리이 패키지는 일을 –

1
 $url = "http://example.com/123.jpg"; 
     $url_arr = explode ('/', $url); 
     $ct = count($url_arr); 
     $name = $url_arr[$ct-1]; 
     $name_div = explode('.', $name); 
     $ct_dot = count($name_div); 
     $img_type = $name_div[$ct_dot -1]; 

     $destinationPath = public_path().'/img/'.$name; 
     file_put_contents($destinationPath, file_get_contents($url)); 

이 원하는 whereever 다음 저장, 파일 이름은 위의 경우에 123.jpg되어 원래의 파일 이름이됩니다.

가져 오기 이미지 이름이 당신에게 많은 도움이 될 것입니다 경우 모르겠어요 here

10

에서 언급하지만 당신은 Intervention Library보고 할 수 있습니다. 원래 이미지 조작 라이브러리로 사용하기위한 것하지만 URL에서 이미지를 저장 제공 : 새로운 $가있다

$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg'); 
+0

보다, 파일 이름이 될 수 있습니다. – Ali

관련 문제