Hi
I'm trying to create a file and write some simple text to it, but it never seeems to work. This is my code:

<?php
$file = fopen("http://servername/directory/type4.txt", "w");
fputs($file,"test");
fclose($file);
?>

It keeps saying that there is an invalid file handle. If type4.txt does not exist, it should create one, correct?

Cheers

    You shouldn't be able to write to an HTTP file handle. If you writing to the same system your script is on the put in the full server path to the file.

      i've tired that too. I'm assuming the full server path is of the form:

      "/usr/www/users/filename.txt"

      Thanks

        If you supply fopen() with a valid local filename and you are denied access, you probably are running into a basic security precaution that is normal for any multiuser system. The Web server is likely to be running under a special user identity, not yours. If you create a directory and do not grant permission to the Web server to write there, it won't be able to do so. You need to get a basic book on administration of whatever operating system you are using and learn how it works. This isn't really a PHP problem.

          Steve
          Thank you for your gracious response. I'll contact the ISP we're engaging for this project.

            agreed, it's almost definitely a permissions problem: if apache is running under the name "hyphen" (just an example) then any file you want to write to or any directory you want to write to must either
            a) be owned by hyphen and have owner privleges set to write or,
            b) be owned by anyone and have all priveleges set to write

            1. find out what name apache is running under with:
              [root]# ps -ef | grep "http"
              or, on bsd systems
              [root]# ps aux | grep "http"
            2. su to root and change the name of the file you want to write to to that name, vize
              [root]# chown hyphen ./foo
            3. make sure that write permissions are set for said file:
              [root]# ls -l | grep foo
              will return something like
              -r--r--r-- hyphen 234 34345 foo
              the r's stand for "read" and the dashes (except for the first one) stand for write and execute respectively. there are three clusternigs of rwx which represent the permissions for the file's owner, group, and rest-of-world respectively. the dashes mean those permissions are off.
            4. turn writing on: you want the owner to be able to write to the file so you do:
              [root]# chmod o+w ./foo
              which means add w(rite) for o(wner). ls -la will now give:
              -rw-r--r-- hyphen 123 2345 foo
              meaning the owner (hyphen) can write to this file....

            you may also need to adjust the permissions on the directory in which the file resides

            there are a lot of sites that do a far better job of explaining permissions than i've given here.

            -frymaster

              In addition, your code example is inherently flawed:

              &nbsp;&nbsp;$file = fopen("http://servername/directory/type4.txt", "w");
              &nbsp;&nbsp;fputs($file,"test");
              &nbsp;&nbsp;fclose($file);

              although PHP is safe enough to test the file handle internally instead of just trying to dereference a NULL pointer like C or C++ would!

              Since fopen() is not guaranteed to succeed, you really should be saying:

              &nbsp;&nbsp;if ($file = fopen("http://servername/directory/type4.txt", "w"))
              &nbsp;&nbsp;&nbsp;&nbsp;{
              &nbsp;&nbsp;&nbsp;&nbsp;fputs(...);
              &nbsp;&nbsp;&nbsp;&nbsp;fclose(...);
              &nbsp;&nbsp;&nbsp;&nbsp;}
              else
              &nbsp;&nbsp;&nbsp;&nbsp;{
              &nbsp;&nbsp;&nbsp;&nbsp;echo "Oops, can't do it";
              &nbsp;&nbsp;&nbsp;&nbsp;// or whatever other error logging/recovery you
              &nbsp;&nbsp;&nbsp;&nbsp;// might prefer over PHP's default response, which
              &nbsp;&nbsp;&nbsp;&nbsp;// is to just barf the error message directly to
              &nbsp;&nbsp;&nbsp;&nbsp;// the output.
              &nbsp;&nbsp;&nbsp;&nbsp;}

              In many cases, I have my code email me a complaint when something like this goes wrong, in addition to issuing some kind of gentle apology to the user of the site.

                yes..I know I'm supposed to check for an error, in case the file could not be opened.written to. I just included the code specific to fopen() to explain the exact problem.

                  Write a Reply...