file_get_contents() or copy() in php4
Moderator: General Moderators
file_get_contents() or copy() in php4
Does anyone know of a way to retrieve files from another web site in php4? I have tried file_get_contents() and copy() and have had nothing but headaches. I know that this is possible in php5, but I'm looking for a solution that can be used by both php4 and php5 supported servers. If anyone has any ideas as to how to do this, it would be appreciated.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I keep crashing my local server and getting this error on another:
Warning: file_get_contents(http://www.neo-geek.net/favicon.ico): failed to open stream: No route to host in /home2/neogeek/public_html/file.php on line 3
And this is the code that is returning that error:
Warning: file_get_contents(http://www.neo-geek.net/favicon.ico): failed to open stream: No route to host in /home2/neogeek/public_html/file.php on line 3
And this is the code that is returning that error:
Code: Select all
<?php
file_get_contents('http://www.neo-geek.net/favicon.ico');
?>You might want to verify if your webhosts allows outgoing connections first.. (eg: can you ping the 'external' domain?)
(http://www.php.net/curl would be the de-facto standard for performing http requests with php)
(http://www.php.net/curl would be the de-facto standard for performing http requests with php)
Thanks I will look into that. However I am trying to write a code that will be distributed to different people that might not have access to adding plugins to their servers. Is there any other possible way to retrieve files using php4? Something like this seems like it should be very simple (like using copy(), but only in php5). I hope that if there is a solution that it is found here because I can imagine that I am not the only one who has run into a problem such as this.
During trying to get this to work I came across this. I looked at my local and remote php.ini values using ini_get('allow_url_fopen ') and the value was set at 1. Could there be other forces at work here besides just allow_url_fopen?
Thank you everyone for your help with this matter. After some more messing around in PHP I think I finally have a solution:
Code: Select all
define('lnbr', "\n");
function fetch_remote_file($file) {
$path = parse_url($file);
$fs = @fsockopen($path['host'], 80);
if ($fs) {
$header = 'GET ' . $path['path'] . ' HTTP/1.0' . lnbr;
$header .= 'Host: ' . $path['host'] . str_repeat(lnbr, 2);
fwrite($fs, $header);
$buffer = '';
while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; }
preg_match('/Content-Length: ([0-9]+)/', $buffer, $matches);
return substr($buffer, -$matches[1]);
} else { return false; }
}