create file and write content in same code block

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • leon.san.email@gmail.com

    create file and write content in same code block

    This a modified part of a larger script I made. I couldn't get it
    working so I made a smaller part of it - hoping to get it working.
    Well it still doesn't work :(
    The following code creates the new file, but does not write the
    $content strings to the file. It then prints "Did nothing!" instead of
    "done!".

    <?php

    $word = "2002";

    $content1 = "one ";
    $content2 = "two ";
    $content3 = "three";

    $fileName = '/home/leke/www/dic/' . $word . '.html';
    $handle = fopen($fileName , 'w');
    if (file_exists($f ileName)) {
    print "Did nothing!";
    }
    else
    {
    fwrite($handle, "$content1 . $content2 . $content3");
    fclose($handle) ;
    print "done!";
    }
    ?>

    I cant figure out the problem. Can anyone help me and maybe explain
    what went wrong?
    Thanks :)
  • macca

    #2
    Re: create file and write content in same code block

    Maybe you need to change the permissions for the '/home/leke/www/dic/'
    directory to allow write.

    Comment

    • Robert Thomason

      #3
      Re: create file and write content in same code block

      leon.san.email@ gmail.com wrote:
      This a modified part of a larger script I made. I couldn't get it
      working so I made a smaller part of it - hoping to get it working.
      Well it still doesn't work :(
      The following code creates the new file, but does not write the
      $content strings to the file. It then prints "Did nothing!" instead of
      "done!".
      >
      <?php
      >
      $word = "2002";
      >
      $content1 = "one ";
      $content2 = "two ";
      $content3 = "three";
      >
      $fileName = '/home/leke/www/dic/' . $word . '.html';
      $handle = fopen($fileName , 'w');
      if (file_exists($f ileName)) {
      print "Did nothing!";
      }
      else
      {
      fwrite($handle, "$content1 . $content2 . $content3");
      fclose($handle) ;
      print "done!";
      }
      ?>
      >
      I cant figure out the problem. Can anyone help me and maybe explain
      what went wrong?
      Thanks :)
      The fopen($filename ,'w') function will create the file. Therefore, the
      file exists, and file_exists() will return true, causing the code to
      take the "Did nothing" branch.

      Try this:

      $fileName = '/home/leke/www/dic/' . $word . '.html';

      if (file_exists($f ileName)) {
      print "Did nothing!";
      }
      else
      {
      $handle = fopen($fileName , 'w');
      fwrite($handle, "$content1 . $content2 . $content3");
      fclose($handle) ;
      print "done!";
      }

      Of course, you probably want to add some error checking after the
      fopen() call to make sure the file was created properly before writing
      to it, but this sequence should work.

      Comment

      • leon.san.email@gmail.com

        #4
        Re: create file and write content in same code block

        Ok, I see what you mean. Originally I thought because fopen($fileName ,
        'w'); was assigned to $handle , it wouldn't be executed until $handle
        was called in fwrite($handle, ...

        Thanks for pointing that out :)

        Comment

        Working...