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
    10
    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 07:38 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,234
    Thanks
    10
    Thanked 270 Times in 269 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,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    unwiredcoder (05-19-2014)

  • #4
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    10
    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 09:16 PM.

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,234
    Thanks
    10
    Thanked 270 Times in 269 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
    •