1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Fseek Function not working !!!!

Discussion in 'PHP' started by Bhullarz, Feb 18, 2009.

  1. Bhullarz

    Bhullarz New Member

    Code:
    <?php
    $ourFileName="newfile.txt";
    $fh = fopen($ourFileName, 'a+') or die("Can't open file");
    echo " File opened: ".ftell($fh);
    fwrite($fh,"<image></image>");
    fseek($fh,-8,SEEK_END);
    fwrite($fh,"Data Written");
    fclose($fh);
    ?>
    
    Whats wrong here?
    Its output is as follows:
    Code:
    <image></image>Data Written
    
    Where as expected output is:
    Code:
    <image>Data Written</image>
    
    Any Suggestions?
     
    Last edited: Feb 18, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Just try the following.

    Close the file after writing string and then open back to write and see what is the output?
     
  3. Bhullarz

    Bhullarz New Member

    Even after closing the file, output is same. I just forgot to put fclose here. In file, I am using that one. I have edited the post.

    More over, whenever I re-run the PHP page, it should return the current pointer position which should be the at the end here. But ftell function returns 0 everytime, whether text file has data or not.
     
    Last edited: Feb 18, 2009
  4. shabbir

    shabbir Administrator Staff Member

    When you open the file in append mode it always writes to the end of the file and not where the FILE_POINTER
     
  5. Bhullarz

    Bhullarz New Member

    Sir if so , why ftell() function is always resulting 0 whenever it is re-opened. It should give the last point from where it has to start writing. Moreover I am using fseek() function to move the pointer to -8 from end-of-file.
     
  6. shabbir

    shabbir Administrator Staff Member

    Probably what I can get is the file is opened and 0 is set at the last or something like that so that you can not do anything to the previous data.
     
  7. Bhullarz

    Bhullarz New Member

    In this case, how can we edit a text file instead of just writing the data at the end of the file?
     
  8. shabbir

    shabbir Administrator Staff Member

    Probably r+
     
    Last edited: Feb 18, 2009
  9. Bhullarz

    Bhullarz New Member

    Ok. I used r+ and added some more logic to it.
    Code:
    <?php
    
    $ourFileName="newfile.txt";
    
    if(is_file($ourFileName))
    	$fh = fopen($ourFileName, 'r+') or die("Can't open file");
    else
    	$fh = fopen($ourFileName, 'w') or die("Can't open file");
    
    fwrite($fh,"<image></image>");
    fseek($fh,-8,SEEK_END);
    fwrite($fh,"Data Written");
    fclose($fh);
    
    ?>
    
    But still I am not getting expected result.
    now the result is as follows:
    Code:
    <image>Data Written
    
    [/code]
     
  10. shabbir

    shabbir Administrator Staff Member

    PHP does not have a function to insert text into the middle of a file and you have to read the complete data and then insert the data in the variables and write it back. Thats the best way to go about getting this done.
     
  11. Bhullarz

    Bhullarz New Member


    Okay sir ! Thanks.
     

Share This Page