В заметке приводится несколько примеров удалённого вывода файла на php. Разбираем, как вывести содержимое файла на сервере, когда он находится на другом удаленном сервере.
Выводим содержимое текстового файла на главной странице:
<?php
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
function is_main($url) {
return preg_match("/^(http:\/\/)?([\w-]+\.)+[a-z]{2,4}(\/|\/[\w-]*\.[a-z]{1,10})?$/i", $url);
}
if (is_main($url))
{
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
echo $text;
}
@fclose($fp);
}
}
?>
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
function is_main($url) {
return preg_match("/^(http:\/\/)?([\w-]+\.)+[a-z]{2,4}(\/|\/[\w-]*\.[a-z]{1,10})?$/i", $url);
}
if (is_main($url))
{
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
echo $text;
}
@fclose($fp);
}
}
?>
Выводим содержимое файла на главной странице с учетом кодировки:
<?php
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
function is_main($url) {
return preg_match("/^(http:\/\/)?([\w-]+\.)+[a-z]{2,4}(\/|\/[\w-]*\.[a-z]{1,10})?$/i", $url);
}
if (is_main($url))
{
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
$tt = iconv("Windows-1251", "UTF-8", $text);
if ($tt) {
echo $tt;
} else {
echo $text;
}
}
@fclose($fp);
}
}
?>
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
function is_main($url) {
return preg_match("/^(http:\/\/)?([\w-]+\.)+[a-z]{2,4}(\/|\/[\w-]*\.[a-z]{1,10})?$/i", $url);
}
if (is_main($url))
{
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
$tt = iconv("Windows-1251", "UTF-8", $text);
if ($tt) {
echo $tt;
} else {
echo $text;
}
}
@fclose($fp);
}
}
?>
Выводим содержимое файлов на всех страницах сайта:
<?php
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
echo $text;
}
@fclose($fp);
}
?>
$host = "www.ploshadka.net";
$link = "/folder/ploshadka.txt";
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET ".$link." HTTP/1.0\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
@fwrite($fp, $out);
$text = "";
do {
$text .= @fgets ( $fp, 4096 );
} while ( strpos ( $text, "\r\n\r\n" ) === false );
$ferror = strpos($text, "404 Not Found");
$text = "";
if ($ferror === FALSE) {
while (!feof($fp)) {
$text.= @fgets($fp, 128);
}
echo $text;
}
@fclose($fp);
}
?>