Click to See Complete Forum and Search --> : Could someone help with fread(url)... ?


mitya
11-03-2003, 07:25 AM
No matter how hard I try I cannot return the content of a remote source using:

$resource = "http://www.somesite.com/page.html";
$pointer = fopen($resource, "r"); //also tried "rb"
$contents = fread($pointer, filesize($resource);
echo $contents;


I have allow_url_fopen set to ON in my php.ini file so I really can't see the problem.

I know it can be done, i.e. return the code of another page (it's an innocent intent), but I cannot get it to work.

Any help greatly appreciated. Cheers.

pyro
11-03-2003, 07:39 AM
I would do it more like this (untested):

<?PHP
$file = "http://www.domain.com/file.htm";
$contents = file($file);
foreach ($contents as $line) {
echo str_replace("<", "&lt;", $line);
}
?>

mitya
11-03-2003, 07:43 AM
Grrr... needless to say it works perfect.


The line:

foreach ($contents as $line)

...is a new one on me. What's that literally saying?

Thanks as always.

pyro
11-03-2003, 07:50 AM
$contents is an array, so the foreach construct tells it to iterate through the array. In the example I posted, it is equal to this:

for ($i=0; $i<count($contents); $i++) {
echo str_replace("<", "&lt;", $contents[$i]);
}

You can find more info at http://us4.php.net/manual/en/control-structures.foreach.php.

mitya
11-03-2003, 07:54 AM
Thanks for that :)

pyro
11-03-2003, 07:57 AM
You're very welcome... :)