fread()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    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($fileo pen));
    echo "File size = ".filesize($fil ename); This echo is empty.
    echo "Contents = ".$contents ; This echo is empty.

    fclose($handle) ;

    Ken


  • Steve

    #2
    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

    Comment

    • Michael Fesser

      #3
      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.


      [color=blue]
      >$contents = fread($fileopen , filesize($fileo pen));[/color]

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

      This should be

      fclose($fileope n);


      You could also try this simple code instead:

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



      HTH
      Micha

      Comment

      Working...