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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2007
    Location
    England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP File Write Error

    Hi, i have a php code script running on my website, the problem i have is that it will not write data to the text file even though it exists?

    error is:

    The file words.txt is not writable

    script below:

    <?php
    $filename = 'words.txt';
    $somecontent = $_GET['words'];

    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
    echo "Cannot open file ($filename)";
    exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

    } else {
    echo "The file $filename is not writable";
    }
    ?>


    Can anyone help me?

    BlackFish

  • #2
    New Coder
    Join Date
    Apr 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this
    PHP Code:
    <?php
    $filename 
    'words.txt';
    $somecontent $_GET['words'];

    // Let's make sure the file exists and is writable first.
    if(!is_writable($filename))
      {
        
    chmod($filename777);
      }
    else{

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle fopen($filename'a')) {
    echo 
    "Cannot open file ($filename)";
    exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle$somecontent) === FALSE) {
    echo 
    "Cannot write to file ($filename)";
    exit;
    }

    echo 
    "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

    } else {
    echo 
    "The file $filename is not writable";
    }
    ?>

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Also check this ...
    Some webhosts have disabled fopen,fwrite for security reasons.
    Make sure your webhost has it enabled.


  •  

    Posting Permissions

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