PHP Velho Oeste 2024

원격 파일 사용하기

php.ini에서 allow_url_fopen을 활성화하면, 대부분의 함수에서 파일명 인수로 HTTPFTP URL을 사용할 수 있습니다. 이와 더불어 URL을 include, include_once, require, require_once 구문에서 사용할수 있다. (PHP 5.2.0부터, 이 기능을 사용하려면 allow_url_include 를 활성화해야 합니다) PHP가 지원하는 프로토콜에 대한 정보는 Supported Protocols and Wrappers를 참고하십시오.

Note:

PHP 4.0.3 이하에서 URL 래퍼(wrapper)를 사용하기 위해서는 configure 옵션으로 --enable-url-fopen-wrapper 을 명시할 필요가 있다.

Note:

PHP 4.3 이하 버전의 PHP 윈도우 버전은 다음 함수에서 원격 파일 접근을 지원하지 않는다: include, include_once, require, require_once, 그리고 GD and Image 함수 목록 내의 imagecreatefromXXX 함수.

예를 들어, 이 기능을 사용하여 원격 웹 서버가 출력하는 내용을 파일로 열고, 그 출력 내용에서 원하는 데이타를 분석하여, 이 원하는 데이타로 데이타베이스 질의에 사용하거나, 웹 사이트에 맞는 모양으로 변형 시켜 출력할 수 있다

Example #1 원격 페이지의 제목을 가져오기

<?php
$file 
fopen ("http://www.php.net/""r");
if (!
$file) {
    echo 
"<p>Unable to open remote file.\n";
    exit;
}
while (!
feof ($file)) {
    
$line fgets ($file1024);
    
/* This only works if the title and its tags are on one line */
    
if (preg_match ("@\<title\>(.*)\</title\>@i"$line$out)) {
        
$title $out[1];
        break;
    }
}
fclose($file);
?>

해당 서버에 권한이 있는 사용자로 접속할수 있다면 FTP를 이용해 파일에 작성할 수도 있다. 이 방법으로 새로운 파일만 생성할수 있다. 기존의 파일을 덮어쓰려고 하면, fopen() 호출시에 실패하게 될것이다.

'anonymous'가 아닌 사용자로 접속하려면, URL내에 username을 (필요하다면 password도) 다음과 같이 명시해야 한다 : 'ftp://user:password@ftp.example.com/path/to/file'. (또한 HTTP에서 Basic authentication을 사용한 인증을 요구하는 경우에도 이와 같은 문법을 사용할 수 있다.)

Example #2 원격 서버에 데이터 저장하기

<?php
$file 
fopen ("ftp://ftp.php.net/incoming/outputfile""w");
if (!
$file) {
    echo 
"<p>원격 파일을 쓰도록 열 수 없습니다.\n";
    exit;
}
/* 여기에서 데이터를 씁니다. */
fwrite ($file"$HTTP_USER_AGENT\n");
fclose ($file);
?>

Note:

위의 예제를 보고, 이 테크닉을 사용하여 remote log를 작성할 수 있겠다고 생각할 수도 있다. 그러나 불행하게도 원격 파일이 이미 존재하면 fopen() 호출은 실패할것이기 때문에 동작하지 않을것이다. 그와 같은 분산 log를 수행하려면 syslog()를 참고하도록 한다.

add a note add a note

User Contributed Notes 4 notes

up
8
slva dot web dot sit at gmail dot com
10 years ago
If  allow_url_fopen is disabled in php.ini you can use CURL function for check file exist:

<?php
$ch
= curl_init("http://www.example.com/favicon.ico");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
curl_close($ch);
?>
up
-4
anon
4 years ago
The following functions can't access external files if allow_url_fopen is disabled:

- fopen
- file_put_contents
- copy

I don't know if this is documented anywhere else, but I couldn't find it. Feel free to delete this post if so.
up
-33
kalidass dot jst at gmail dot com
8 years ago
public function get_url($request_url) {

$curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $request_url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    $JsonResponse = curl_exec($curl_handle);
    $http_code = curl_getinfo($curl_handle);

  return($JsonResponse);
}
get_url("http://www.example.com");
up
-70
heck at fas dot harvard dot edu
19 years ago
The previous post is part right, part wrong. It's part right because it's true that the php script will run on the remote server, if it's capable of interpreting php scripts. You can see this by creating this script on a remote machine:
<?php
echo system("hostname");
?>
Then include that in a php file on your local machine. When you view it in a browser, you'll see the hostname of the remote machine.

However, that does not mean there are no security worries here. Just try replacing the previous script with this one:
<?php
echo "<?php system(\"hostname\"); ?>";
?>
I'm guessing you can figure out what that's gonna do.

So yes, remote includes can be a major security problem.
To Top