development

PHP-URL에서 직접 내 서버로 이미지 복사

big-blog 2020. 11. 19. 21:45
반응형

PHP-URL에서 직접 내 서버로 이미지 복사


중복 가능성 :
PHP를 사용하여 PHP URL에서 이미지 저장

다음을위한 PHP 코드를 갖고 싶습니다.

하나의 이미지 URL (예 : http://www.google.co.in/intl/en_com/images/srpr/logo1w.png) 이 있다고 가정합니다 .

하나의 스크립트를 실행하면이 이미지가 복사되어 777 권한이있는 폴더 내 서버에 저장됩니다.

가능합니까? 그래, 당신도 같은 방향을 제시해 주시겠습니까?

감사,

이안


두 가지 방법, PHP5 (또는 그 이상)를 사용하는 경우

copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.jpeg');

그렇지 않은 경우 file_get_contents를 사용하십시오.

//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);

에서 이 SO 포스트


에서 복사 이미지 URL에서 서버로, 이후 모든 이미지를 삭제

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('tmp/'.$imagename,$image);       

$url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents=file_get_contents($url);
$save_path="/path/to/the/dir/and/image.jpg";
file_put_contents($save_path,$contents);

당신은 allow_url_fopen설정 해야합니다on


SO 스레드 는 문제를 해결할 것입니다. 요약 :

$url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png';
$img = '/my/folder/my_image.gif';
file_put_contents($img, file_get_contents($url));

$url = "http://www.example/images/image.gif";
$save_name = "image.gif";
$save_directory = "/var/www/example/downloads/";

if(is_writable($save_directory)) {
    file_put_contents($save_directory . $save_name, file_get_contents($url));
} else {
    exit("Failed to write to directory "{$save_directory}");
}

참고URL : https://stackoverflow.com/questions/6306935/php-copy-image-to-my-server-direct-from-url

반응형