source

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

goodcode 2022. 12. 25. 16:57
반응형

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

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

나는 PHP 코드를 가지고 싶다.

이미지 URL이 1개 있다고 가정합니다.예를 들어 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.png');

그렇지 않으면 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.png", "w");
fwrite($fp, $content);
fclose($fp);

이 SO 투고부터

Copy images from url to server에서 다음 시간 이후 모든 이미지를 삭제합니다.

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

반응형

'source' 카테고리의 다른 글

Node.js에서 SQL 주입 방지  (0) 2023.01.09
사용된 인덱스 콜룬 목록(mysql/mariadb)  (0) 2023.01.09
PHP 작업 비동기 실행  (0) 2022.12.25
Windows의 MariaDB - 시작 도움말?  (0) 2022.12.25
MySQL에서 최적의 잠금 기능  (0) 2022.12.25