views:

148

answers:

1

hi i'm programming php using:

  • netbeans 6.8
  • lampp for ubuntu (xampp)
  • apache which came with xampp

$fh = fopen("testfile2.txt", 'w') or die("Failed to create file"); $text ="hello man cool good"; fwrite($fh, $text) or die("Could not write to file"); fclose($fh); echo "File 'testfile.txt' written successfully";

//i get the next error:

Warning: fopen(testfile2.txt) [function.fopen]: failed to open stream: Permission denied in /home/marmoush/allprojects/phpprojects/myindex.php on line 91
    Failed to create file 

anyway i know what this error is; it's about folder and files permissions; i looked into the folder permission tab made access available for "others" group ( to read and write) the program worked result was a file (test.txt) so i looked at the created file permission it appears to be that (php , xampp or whoever) creates file with (nobody permission)


I have 2 QUESTIONS:

1- what if i need the file created by (php code and xampp ) to have the "root or user or myname" permissions ?? where to set this setting

2-also my concern (what if i send this files to actual web server will it make nobody permissions also nobody ? when they create files

A: 

1)

Php has chmod and chown functions, that simply wrap the linux commands; this does not mean that you can arbitrarily change the files' permissions and ownership from any PHP script.

If you need a file to be owned by a specific user, you need to run apache as root, but this is WRONG: it is an ugly idea to run Apache with super user rights.

It seems that your problem is that the directory belongs to your user and apache runs (correctly) as nobody, so it cannot write there. If you are developing in your own box simply chmod a+rwx the directory that contains your PHP sources otherwise write your files in a temporary directory, such as /tmp/ where anybody can read/write.

2)

If you are going to use a shared hosting, you have to check your ISP configuration: in many cases the hosted Apache can write in your directories (so that applications like Wordpress can install plugins and update themselves).

Iacopo