Page 1 of 1

Ways to read a file from an external host

Posted: Fri Aug 01, 2008 7:59 am
by ngungo
Problem: to read a file http://somesite.com/example.txt
I know a few ways to this but none would work for all circumstances.

1.

Code: Select all

 
$file = 'http://somesite.com/example.txt';
$source = file_get_contents($file);
 
It won't work on those hosts that allow_url_fopen not turn 'On'.

2.

Code: Select all

 
$file = 'http://somesite.com/example.txt';
system('wget ' .$file. ' > temp.txt');
$source = file_get_contents($temp.txt);
 
or

Code: Select all

 
$file = 'http://somesite.com/example.txt';
system('curl ' .$file. ' > temp.txt');
$source = file_get_contents($temp.txt);
 
It won't work on those hosts that would not allow 'write permision' to a file.

3.

Code: Select all

 
$file = 'http://somesite.com/example.txt';
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$source = curl_exec($ch);      
curl_close($ch);
 
This won't work on those hosts that have no cURL extension installed.

I run out of ways. Any hints would be appreciated. Thanks!
--ngungo

Re: Ways to read a file from an external host

Posted: Fri Aug 01, 2008 12:49 pm
by JAB Creations
I'd try fopen though I'm not sure if that will work over HTTP. I'd also have the host install the cURL function for PHP otherwise I'd find a new host. Good luck!

Re: Ways to read a file from an external host

Posted: Sun Aug 03, 2008 1:55 pm
by pkbruker
fopen() works perfectly over HTTP (and FTP too!)

Try:

Code: Select all

 
$f = fopen('http://www.example.com', 'r');
print stream_get_contents($f);
fclose($f);
 

Re: Ways to read a file from an external host

Posted: Mon Nov 10, 2008 11:03 am
by purepersian
Hi there guys,
I am new to this forums and I'm in need of an answer as soon as possible.
I want to use this piece of code:

Code: Select all

$file = 'http://www.fileden.com/files/2008/11/7/2177439/24.zip';
header('Content-type: application/force-download');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="'. basename($file) .'"');
header('Content-length: '. filesize($file) );
readfile($file);
 

and I know readfile is the crappiest way out there to dump out the contents of a file, but the file in question is about 100KB.
I have had my host admin set allow_url_fopen to On and also safe_mode is set to Off. But readfile returns false. The same goes for fopen.
What else should I do to get the script working?
I've also searched around this forum, but I didn't find anything of interest, the closest was this topic.
Maybe something was wrong with my search :D
As additional resources to follow up with my question, I might add that here is a link to my phpinfo: http://dl.book-worms.net/test/info.php

Re: Ways to read a file from an external host

Posted: Mon Nov 10, 2008 12:21 pm
by mmj
In case you haven't noticed this is quite an old thread. :)

Make sure fopen wrappers are on.

Re: Ways to read a file from an external host

Posted: Mon Nov 10, 2008 12:53 pm
by purepersian
Yeah I did notice the dust everywhere in the topic :D
But I thought starting a new topic about something so old would be inappropriate.
Anyway, how can I make sure the wrappers are On?
Is there something I should set in the php.ini? (other than setting the "allow_url_fopen" to On, which is already set as such)
You see, the problem is that the output of "php.ini"s look the same for my local (http://localhost/test/index.php) and remote test files (http://dl.book-worms.net/test/info.php).
But the latter won't allow me to open a remote file with fopen

Re: Ways to read a file from an external host

Posted: Mon Nov 10, 2008 3:45 pm
by Syntac
You can try sending an HTTP request with fsockopen(). Here's a basic example (error checking is not included).

Code: Select all

$data = "";
$fp = fsockopen( "www.example.com", 80 );
fwrite( $fp, "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n" ); // Minimal HTTP client request.
while( ( $read = fread( $fp, 1024 ) ) != "" ) // Reading stops when a packet is exhausted, so we need to do it until all the data has been transferred.
    $data .= $read;
 
$data = explode( "\r\n\r\n", $data ); // $data[0] will be headers, $data[1] will be page data.
$page_headers = explode( "\r\n", $data[0] ); // Headers are separated by "\r\n".
$page_data = implode( "\n\n", array_slice( $data, 1 ) ); // There may be more "\r\n\r\n"s, so we need to merge all the remaining sections. Using "\n\n" because CRLFs are stupid.
 
// Do stuff with $page_data.

Re: Ways to read a file from an external host

Posted: Tue Nov 11, 2008 12:48 pm
by purepersian
Thanks Syntac for your detailed reply.
I tried it, but I get "Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"
Am I doing something wrong?
And am I getting the meaning of this script right?
On line 2 you are opening a connection to the remote file, in my case "http://www.fileden.com/files/2008/11/7/2177439/24.zip".
On line 3 you are submitting the HTTP request.
And the rest is just reading the stuff from the file.
Is that it?
I think it is actually quite straightforward, but I don't know why I'm running into problems!

Re: Ways to read a file from an external host

Posted: Tue Nov 11, 2008 1:15 pm
by Syntac
HTTP is not a socket protocol. It operates over TCP, so...

Code: Select all

$fp = fsockopen( "tcp://www.example.com", 80 );
// Or you can just leave the transport out:
$fp = fsockopen( "www.example.com", 80 );

Re: Ways to read a file from an external host

Posted: Wed Nov 12, 2008 2:14 am
by purepersian
Thanks again. When I leave out the transport, or replace it with TCP, I get the following:
php_network_getaddresses: getaddrinfo failed: Name or service not known
I think using the fopen method must be the right thing to do. Because in this topic I found somebody who wanted to basically do the same thing as I. I have hence implementing the same method. The only problem is that despite allow_url_fopen being On, I seem to be getting errors opening a remote file.
That is my biggest problem.

Re: Ways to read a file from an external host

Posted: Wed Nov 12, 2008 2:35 am
by mmj
You really need to talk to your host imo.

Re: Ways to read a file from an external host

Posted: Wed Nov 12, 2008 2:43 am
by purepersian
I guess the people at my host are a bunch who don't know anything about PHP. Because I gave them all the details and they just stared at me dumbly. :(