Connecting Tech Pros Worldwide Help | Site Map

fread()

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 08:41 AM
Ken
Guest
 
Posts: n/a
Default fread()

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, 08:41 AM
Steve
Guest
 
Posts: n/a
Default 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, 08:41 AM
Michael Fesser
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.