I'm trying to make a very basic CMS for my website. I'm using forms to fill in all the information. Everything works upto the file creation. I know this because if I just say echo $page it will display exactly what I want. Here is the code that I'm using to write to a file:

$filename = $filename . ".php";

//Line 101
$filehandler = fopen($filename, 'w') or die("Can't Open File");
$fwrite($filehandler, $page);
fclose($filehandler);

I get this error:

Warning: fopen(myfile.php): failed to open stream: Permission denied in /home/dvdswapn/public_html/domicile/contribute/finished/submit.php on line 101
Can't Open File

From my understanding, I need to use chmod. So I used CuteFTP and changed the permission to 777 for the "/finished" folder. I still get the same error and when I go back into check the permissions, it is back to its original setting (not 777). I was thinking that maybe it had to do with me trying to create a php file, but I changed it to txt and it still didn't work. Can someone tell me what I am doing wrong?

    i cant understand it either
    but without running your script, does the folder EVER really have 777
    you can test this by Refresh your FTP client, or make a new FTP connection to check

    you can make a simple test,
    by creating a new folder with 777 and in that folder
    run the following example
    from the official php.net fwrite() manual: http://php.net/fwrite

    Example 1. A simple fwrite() example

    <?php
    $filename = 'test.txt';
    $somecontent = "Add this to the file\n";
    
    // 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";
    }
    ?> 

      For some reason, CuteFTP wouldn't change the permissions. I downloaded the trial for WS FTP and it did the trick.

        Write a Reply...