i want to create a textfile database on the fly

$fnew = fopen("database/$account.db", "w+");
flock($fnew, 1);

this does not create a new textfile on the server.
how do i do this??

    i get this error:

    Warning: fopen("database/test.db","w+") - Permission denied in /home/######/########.com/dbase/add.php on line 57

    how do i get permission to create files on the server??

      chmodd the directory to 777

        Originally posted by Skull
        chmodd the directory to 777

        Yes, but that makes the whole directory writable by anyone, which I do not want to happen to my directories.

        I am having the same problems, I chmoded a test directory to create a little text editor app to add files on the fly to my site. This worked fine, but I canot write to any other part of the site.

        Is there a good way to authenticate myself to the server with my user/pass?

          You can write to a directory that is above your public_html directory. I've been told this is not hackproof (I don't know why) but someone won't be able to browse to or access these files over http.

            chmod to 700 or 744, that should work... 700 allows only owner read/write/execute

              Originally posted by seby
              chmod to 700 or 744, that should work... 700 allows only owner read/write/execute

              Right, I understand that, but I am trying to write from a webpage, how can I make the script understand that I have owner permissions?

                php has owner permission, it will be the one telling the server to make the file.. and since the script is basically internal it will only allow the script to write... not sure if that makes sense.

                  17 days later

                  Not sure if it will work with out some if exist routine
                  but do what I do sometimes when I get this and thats
                  open notepad and save out a blank file e.g. myfile.dat
                  i.e. whatever your php is trying to create filewise.

                  up that to your webspace then chmod the dat file to
                  write accress.

                  Then run the php , if all is well it will write to the file.

                    2 months later

                    I believe you have to change owner permissions on the directory example: chown directory_name owners_name

                    This is safer than just changing the chmod because while it may allow 777 access if you choose. it only allows that access to a certain user.

                      a year later

                      I don't understand any of this...

                      What does this mean??

                      chmodd the directory to 777

                      I want to write a bunch of files out from a PHP script that I wrote that queries my database. I am trying to write BRAND new files that do not already exist. They are playlists of media files and there are over 500+ (all legal mind you). Is there a way to do this? I assume there is and that y'all explain it, but i just don't understand what all this chmodd stuff is.

                      Thanks.

                      vs

                        Wow! This is a very old thread.

                        Since my question I have been able to write to text files. chmod is an ftp function that will change the mode (read-write permissions) of a file. Although I know what thsi is, I couldn't figure out how to use it and then I discovered I didn't really need to do it anyway.

                        Turns out php ususally has permissions to write to a directory on the web server.

                        Here is the code I use:

                            // The file will be prepended.
                            if (!$handle = fopen($filename, 'a')) {
                                 print "Cannot open file ($filename)";
                                 exit;
                            }
                        
                        // Write $somecontent to our opened file.
                        if (!fwrite($handle, $somecontent)) {
                            print "Cannot write to file ($filename)";
                            exit;
                        }
                        
                        fclose($handle);
                        
                          5 months later

                          I'm really new to this, sorry;

                          I've been having the same problem; I just tried gmdavis' script, and it only seems to work when chmod is 766, writable by anyone. (!) This is on a remote server (hosting company) with safe mode enabled. Is this going to be a security problem, and is there any alternative? (I just did this from an FTP client; maybe chown would help? How can I set that up?)

                          Thanks a lot!

                            Some things to try:
                            1. Did you create the file then upload it before trying to write to it? if so the owner is not the same as the script. (The script will do things as "nobody" or some other system user.) So if you can get a script to create the (empty?) file to begin with, the owner will be the same and the permissions will not be an issue.

                            1. Another thing: if you can, put the file above the html root (usually public_html). Directories above this level cannot be browsed to so they are fairly safe even if you have to set the permissions to 776.
                              3 months later

                              I'm not sure how valid it is, but if you have a secure page to do it from, you could use:

                              <?php
                                 if(@$_POST['write']){ 
                                        $filename =@$_POST["create"];
                                        $handle = fopen($filename, "w+");
                                        $contents = "New File";
                                        fwrite($handle, "$contents");
                                        fclose($handle);
                              
                              
                               } else {
                              echo "<form method=\"post\" action=\"file_edit.php\">
                                              Create File: <input type=\"text\" name=\"create\"><br>
                                              <input type=\"submit\" name=\"write\" value=\"Create\">
                                          }</form>";
                              ?>

                              That would create files in the directory that the edit_file.php file was in, granted you could easily change that. That way you could define the name of the txt file each time.

                                Write a Reply...