Connecting Tech Pros Worldwide Help | Site Map

fread()

  #1  
Old July 17th, 2005, 09:41 AM
Ken
Guest
 
Posts: n/a
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


  #2  
Old July 17th, 2005, 09:41 AM
Steve
Guest
 
Posts: n/a

re: fread()



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

  #3  
Old July 17th, 2005, 09:41 AM
Michael Fesser
Guest
 
Posts: n/a

re: fread()


.oO(Ken)
[color=blue]
>I am trying to read a JavaScript file.
>Permissions have been set to 755.
>
>Do I have to open the file first?[/color]

Yes.
[color=blue]
>$fileopen = fopen($filename, "rb");
>echo "File open = ".$fileopen; This echoes Resource id #4
> What is resource id #4?[/color]

fopen() returns an ID that denotes the currently opened file. It's a
handle for all further actions performed on that file.
[color=blue]
> Where do I find a definition of resource id #4?[/color]

Resource-IDs are a special type in PHP.

http://www.php.net/manual/en/languag...s.resource.php
[color=blue]
>$contents = fread($fileopen, filesize($fileopen));[/color]

You can't use filesize() on remote files in PHP4.
[color=blue]
>fclose($handle);[/color]

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
feof(), fseek(), fread() ericunfuk answers 20 March 27th, 2007 10:25 PM
fread 1 byte x N vs N bytes x 1 David Mathog answers 5 October 20th, 2006 02:25 PM
why is fread forgetting where its position? 010 010 answers 13 October 3rd, 2006 12:35 AM
Problem with fread() Alain Lafon answers 10 November 14th, 2005 02:21 AM
Binary files, structs and fread Luc Holland answers 2 November 13th, 2005 03:41 AM