Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    fwrite not writing contents of a string to a text file

    I am having trouble writing a string to a text file. The text file is created fine and it appears to be executing all the code but nothing is writing to the text file as it remains empty when opened. The final echo statement executes as well. Much appreciation for any assistance.


    PHP Code:

    <?php
    $String 
    $array1] . "**" $array1] . "**" $array1] . "**" $array1];
    //To Create File
    $Values 'test.txt';
    // Assigns a variable to the contents of the opened file.
    $EFH fopen$Values'a+' ) or die( 'Cannot open file:. $Values' );
    // If the file with the handle of $EFH is opened 
    if ( $EFH )
      {
        
    // $Values
        
    $Values file_get_contents'test.txt' );
        
    // Check to see if the file is empty.
        
    if ( strlen$Values ) === )
          {
            
    // Add the contents of the $String to the file 
            
    fwrite$Values$String "\n" );
            echo 
    "data written successfully to the file";
          } 
    //strlen( $Values ) === 0
      
    //$EFH
    ?>
    Last edited by unwiredcoder; 05-19-2014 at 08:38 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,444
    Thanks
    11
    Thanked 309 Times in 308 Posts
    The first parameter to fwrite needs to be the file handle you got from fopen. I would also close the file before the end of the script.

  • Users who have thanked tracknut for this post:

    unwiredcoder (05-19-2014)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by unwiredcoder View Post
    PHP Code:
            fwrite$Values$String "\n" ); 
    PHP: fwrite - Manual

    You have no resource handle as your first parameter. Instead you're using the file contents from your $Values = file_get_contents('test'txt') call. You should be using your $EFH resource as the first parameter.

    Or to make life even eassier, you could just use file_put_contents() instead.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    unwiredcoder (05-19-2014)

  • #4
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for the assistance. I also noticed I was overwriting the $Values when I used file_get_contents(). so in the file_put_contents() statement I used test.txt instead of $Values
    Last edited by unwiredcoder; 05-19-2014 at 10:16 PM.

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,444
    Thanks
    11
    Thanked 309 Times in 308 Posts
    You've still got fwrite screwed up, and also file_put_contents. fwrite needs the file handle and the string, two parameters only. File_put_contents needs the file name and the string and the flags. Pick one or the other, but you've got to get the parameters right


  •  

    Tags for this Thread

    Posting Permissions

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