Загрузка файла с сервера и вывод содержимого

123 4
[Удален]
2362

Народ, выручайте! Задавал вопрос на рнр-форумах, но так и не получил ответа, кроме как ссылок на мануалы.....

Мне нужно сделать следующую вещь:

Скрипт загружает текстовый файл с чужого сервера и включает всё его содержимое в документ.

Пробовал сделать через fopen, но показыает ошибку... Все говорят про сокеты.. Но я в рнр не фи га не понимаю :rolleyes:

А вы не подскажите?

tommy-gung
На сайте с 22.11.2006
Offline
304
#1

ReJ, какую ошибку показывает?

Здесь не могла быть ваша реклама
F
На сайте с 12.09.2003
Offline
120
#2

через CURL


function remote_file ($site_url,$referer)
{
global $out_info;
$timeout=10;
$user_agent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
$tmp_result = curl_exec($ch);
curl_close($ch);
if($tmp_result){return $tmp_result;}
else{return false;}
}

include_once(remote_file("твой файл","http://google.com"));

K
На сайте с 12.07.2006
Offline
295
Kpd
#3
Faster:
через CURL

или через file()

$file = file("http://***/file.txt");

foreach ($file as $line) {
echo $line;
}
F
На сайте с 12.09.2003
Offline
120
#4

как показывает практика, через CURL все же лучше :)

K
На сайте с 12.07.2006
Offline
295
Kpd
#5
Faster:
как показывает практика, через CURL все же лучше

Как показывает практика, CURL поддерживают далеко не все хостеры :)

Для сложных (хитрых) операций CURL однозначно полезнее, а для простого парсинга текстового файла достаточно file()

[Удален]
#6
Warning: filesize() [function.filesize]: Stat failed for http://site.ru/file.txt (errno=2 - No such file or directory) in /home/user/public_html/page/script.php on line 3

Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/user/public_html/page/script.php on line 3

Вот что пишет, если, Kpd, использовать твой совет.

Faster - сделал, как написал ты, показывает пустую страницу. Точнее,сделал так:


<?
$site_url = "http://site.ru/file.txt";
$referer = "http://google.com";
function remote_file ($site_url,$referer)
{
global $out_info;
$timeout=10;
$user_agent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
$tmp_result = curl_exec($ch);
curl_close($ch);
if($tmp_result){return $tmp_result;}
else{return false;}
}
?>
F
На сайте с 12.09.2003
Offline
120
#7

добавь echo remote_file ($site_url,$referer);

чтоли...

Kpd,

как показывает практика, хостеры без CURL идут лесом сразу :)

[Удален]
#8
Parse error: syntax error, unexpected T_PRINT, expecting T_STRING in /home/user/public_html/page/script.php on line 4

Теперь пишет.

F
На сайте с 12.09.2003
Offline
120
#9

выведи строчку

echo phpinfo();

там про CURL ченить написано ?

ашипка синтаксиса у тебя

глянь про CURL и полный код в студию

[Удален]
#10

Configure Command

'./configure' '--enable-pic' '--with-libdir=lib64' '--prefix=/usr' '--with-xml' '--enable-bcmath' '--enable-calendar' '--with-curl' '--with-dom' '--with-dom-xslt' '--with-dom-exslt' '--enable-exif' '--enable-ftp' '--with-gd' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr' '--with-xpm-dir=/usr/X11R6' '--with-gettext' '--with-iconv' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-mhash' '--enable-magic-quotes' '--with-mysqli' '--with-mysql=/usr' '--with-snmp' '--enable-ucd-snmp-hack' '--with-openssl' '--enable-discard-path' '--with-pear' '--with-pgsql=/usr' '--with-pspell' '--enable-xslt' '--with-xslt-sablot' '--enable-sockets' '--enable-track-vars' '--with-ttf' '--with-freetype-dir=/usr' '--enable-gd-native-ttf' '--with-xmlrpc' '--with-zip' '--with-zlib'
123 4

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий