SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Remote Source Grabbing Using fopen problems

    Hello All,

    Im wondering if anyone can shed some light on a problem I seem to be enountering when grabbing the source of a page and parsing it into another page.

    PHP Code:
    $WebFile 'http://mydomain.com:49/new.html';
    $LocalFile '/home/mydomain/public_html/requests/SongList.html';

    $handle fopen ($WebFile"r");
    $outhandle=fopen ($LocalFile,"w");
    while (!
    feof($handle)) {
         
    $buffer=fread($handle,4096);
         
    $buffer=str_replace('track=/cgi-bin/request.cgi?ID=','track='$buffer);
         
    fputs($outhandle,$buffer);
    }
    fclose($handle);
    fclose($outhandle); 
    ....is the code Im using.

    It grabs the source once and writes it, but for some reason it seems to be writing it again after that, resulting in two copies of the same data in SongList.html

    Anyone got any ideas why/how its doing it?

    Cheers

  2. #2
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use fgets() in the while loop, because fread() gives you ALL the data upto length (4096 in this case), and does not stop on a new line (like fgets() does).

    PHP Code:
    $WebFile 'http://mydomain.com:49/new.html';
    $LocalFile '/home/mydomain/public_html/requests/SongList.html';

    $handle fopen ($WebFile"r");
    $outhandle=fopen ($LocalFile,"w");
    while (!
    feof($handle)) {
         
    $buffer=fgets($handle,4096);
         
    $buffer=str_replace('track=/cgi-bin/request.cgi?ID=','track='$buffer);
         
    fputs($outhandle,$buffer);
    }
    fclose($handle);
    fclose($outhandle); 

  3. #3
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just notice someone said feof() does not work with URLs... you better just use this method:
    PHP Code:
    $WebFile 'http://mydomain.com:49/new.html';
    $LocalFile '/home/mydomain/public_html/requests/SongList.html';

    $outhandle=fopen ($LocalFile,"w");

    $buffer implode (''file ($WebFile));
    $buffer=str_replace('track=/cgi-bin/request.cgi?ID=','track='$buffer);
    fputs($outhandle,$buffer);

    fclose($outhandle); 

  4. #4
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Its Greatly appreciated

    <edit>
    Hmm, it still is reproducing two versions of the data

    Eg: http://www.br0wnie.com/SongList.html
    Last edited by Brownie; Feb 10, 2002 at 12:43.

  5. #5
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the full version of the file here? You must be doing something wrong.

  6. #6
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the delay

    The Code Im using is:

    PHP Code:
    <?php

    $WebFile 
    'http://domain.com:49/new.html';
    $LocalFile '/home/user/public_html/SongList2.html';

    $outhandle=fopen ($LocalFile,"w");

    $buffer implode (''file ($WebFile));
    $buffer=str_replace('track=/cgi-bin/request.cgi?ID=','track='$buffer);
    fputs($outhandle,$buffer);

    fclose($outhandle);

    echo(
    "<h1>Done</h1>\n<p>I grabbed $WebFile and outputted it to $LocalFile, and did some str_replace's too!</p>");

    ?>
    Cheers
    Last edited by Brownie; Jan 19, 2003 at 07:23.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •