Ways to read a file from an external host

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ngungo
Forum Commoner
Posts: 75
Joined: Thu Jun 08, 2006 10:45 pm

Ways to read a file from an external host

Post 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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Ways to read a file from an external host

Post 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!
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: Ways to read a file from an external host

Post 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);
 
purepersian
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 10:45 am

Re: Ways to read a file from an external host

Post 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
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Ways to read a file from an external host

Post by mmj »

In case you haven't noticed this is quite an old thread. :)

Make sure fopen wrappers are on.
purepersian
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 10:45 am

Re: Ways to read a file from an external host

Post 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
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Ways to read a file from an external host

Post 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.
purepersian
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 10:45 am

Re: Ways to read a file from an external host

Post 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!
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Ways to read a file from an external host

Post 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 );
purepersian
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 10:45 am

Re: Ways to read a file from an external host

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Ways to read a file from an external host

Post by mmj »

You really need to talk to your host imo.
purepersian
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 10:45 am

Re: Ways to read a file from an external host

Post 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. :(
Post Reply