developer tip

HTTP를 통해 원격 서버에서 이미지 복사

optionbox 2020. 10. 9. 11:07
반응형

HTTP를 통해 원격 서버에서 이미지 복사


PHP를 사용하여 원격 서버에서 로컬 폴더로 이미지를 가져 오거나 복사하는 간단한 방법을 찾고 있습니다. 서버에 대한 FTP 액세스 권한이 없지만 모든 원격 이미지는 HTTP (예 : http://www.mydomain.com/myimage.jpg ) 를 통해 액세스 할 수 있습니다 .

사용 예 : 사용자가 자신의 프로필에 이미지를 추가하려고합니다. 이미지가 이미 웹에 있으며 사용자가 직접 URL을 제공합니다. 이미지를 핫 링크하지 않고 내 도메인에서 가져 와서 제공하고 싶습니다.


서버에서 PHP5와 HTTP 스트림 래퍼를 활성화 한 경우 로컬 파일에 복사하는 것은 매우 간단합니다.

copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

이것은 필요한 모든 파이프 라이닝 등을 처리합니다. 일부 HTTP 매개 변수를 제공해야하는 경우 제공 할 수있는 세 번째 '스트림 컨텍스트'매개 변수가 있습니다.


사용하다

$imageString = file_get_contents("http://example.com/image.jpg");
$save = file_put_contents('Image/saveto/image.jpg',$imageString);

PHP에는 파일의 내용을 문자열로 읽는 내장 함수 file_get_contents ()가 있습니다.


<?php
//Get the file
$content = file_get_contents("http://example.com/image.jpg");

//Store in the filesystem. $fp = fopen("/location/to/save/image.jpg", "w"); fwrite($fp, $content); fclose($fp); ?>

파일을 데이터베이스에 저장하려면 $ content 변수를 사용하고 파일을 디스크에 저장하지 마십시오.


다음 네 가지 가능성이 있습니다.

  • 원격 파일 . allow_url_fopenphp.ini에서 활성화 해야 하지만 가장 쉬운 방법입니다.

  • 또는 PHP 설치에서 지원하는 경우 cURL을 사용할 수 있습니다 . 더있다 .

  • 정말 수동으로하고 싶다면 HTTP 모듈을 사용하세요 .

  • 소켓을 직접 사용 하지 마십시오 .


가장 기본적인 방법은 다음과 같습니다.

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";

$rfile = fopen($url, "r");
$lfile = fopen($dir . basename($url), "w");

while(!feof($url)) fwrite($lfile, fread($rfile, 1), 1);

fclose($rfile);
fclose($lfile);

그러나이 작업을 많이 수행하거나 호스트가 원격 시스템에 대한 파일 액세스를 차단하는 경우 CURL을 사용하는 것이 좋습니다. CURL은 더 효율적이고 약간 더 빠르며 더 많은 공유 호스트에서 사용할 수 있습니다.

또한 봇이 아닌 데스크톱처럼 보이도록 사용자 에이전트를 스푸핑 할 수도 있습니다!

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";
$lfile = fopen($dir . basename($url), "w");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);

fclose($lfile);
curl_close($ch);

두 경우 모두 GD를 통해 전달하여 실제로 이미지인지 확인할 수 있습니다.


file_get_contents를 사용하는 것은 매우 간단 합니다. 첫 번째 매개 변수로 URL을 제공하십시오.


폴더를 만들고 적의 이름을 지정하십시오. 예제를 열고 메모장을 열고이 코드를 삽입하십시오.

http://www.google.com/aa.zip파일로 변경 하고 예를 들어 m.php에 저장하십시오.

php 파일을 666으로 chamod하고 폴더를 777로 다운로드합니다.

<?php
define('BUFSIZ', 4095);
$url = 'http://www.google.com/aa.zip';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>

마지막으로 브라우저에서 다음 URL http://www.example.com/download/m.php를 입력 하십시오.

you will see in download folder the file download from other server

thanks


Use a GET request to download the image and save it to a web accessible directory on your server.

As you are using PHP, you can use curl to download files from the other server.


Since you've tagged your question 'php', I'll assume your running php on your server. Your best bet is if you control your own web server, then compile cURL into php. This will allow your web server to make requests to other web servers. This can be quite dangerous from a security point of view, so most basic web hosting providers won't have this option enabled.

Here's the php man page on using cURL. In the comments you can find an example which downloads and image file.

If you don't want to use libcurl, you could code something up using fsockopen. This is built into php (but may be disabled on your host), and can directly read and write to sockets. See Examples on the fsockopen man page.


For those who need to preserve the original filename and extension

$origin = 'http://example.com/image.jpg';

$filename = pathinfo($origin, PATHINFO_FILENAME);
$ext = pathinfo($origin, PATHINFO_EXTENSION);

$dest = 'myfolder/' . $filename . '.' . $ext;

copy($origin, $dest);

This answer helped to me download image from server to client side.

<a download="original_file.jpg" href="file/path.jpg">
  <img src="file/path.jpg" class="img-responsive" width="600" />
</a>

참고URL : https://stackoverflow.com/questions/909374/copy-image-from-remote-server-over-http

반응형