By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,716 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,716 IT Pros & Developers. It's quick & easy.

fread()

P: n/a
Ken
I am trying to read a JavaScript file.
Permissions have been set to 755.

Do I have to open the file first?
fopen($filename, "rb");

Then read it?

My script:

$filename = "http://domainname.com/javascript.js";
echo "File name = ".$filename; This echoes the correct name and path.

$fileopen = fopen($filename, "rb");
echo "File open = ".$fileopen; This echoes Resource id #4
What is resource id #4?
Where do I find a definition of resource id #4?

$contents = fread($fileopen, filesize($fileopen));
echo "File size = ".filesize($filename); This echo is empty.
echo "Contents = ".$contents; This echo is empty.

fclose($handle);

Ken
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

AFAIK you can't request the filesize of a remote file (perhaps in PHP
5+).

Try something like this:

$strURL = 'http://domainname.com/javascript.js';
$strText = '';
$fh = fopen( $strURL, 'r') or die( $php_errormsg );
while( !feof( $fh ) )
{
$strText .= fread( $fh, 1024 );
}
fclose( $fh );
header( 'Content-type: text/plain' );
print $strText;

---
Steve

Jul 17 '05 #2

P: n/a
.oO(Ken)
I am trying to read a JavaScript file.
Permissions have been set to 755.

Do I have to open the file first?
Yes.
$fileopen = fopen($filename, "rb");
echo "File open = ".$fileopen; This echoes Resource id #4
What is resource id #4?
fopen() returns an ID that denotes the currently opened file. It's a
handle for all further actions performed on that file.
Where do I find a definition of resource id #4?
Resource-IDs are a special type in PHP.

http://www.php.net/manual/en/languag...s.resource.php
$contents = fread($fileopen, filesize($fileopen));
You can't use filesize() on remote files in PHP4.
fclose($handle);


This should be

fclose($fileopen);
You could also try this simple code instead:

$filename = 'http://...';
$content = file_get_contents($filename);

http://www.php.net/file_get_contents

HTH
Micha
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.