Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Unable to dynamically create files on the server using PHP



5:15 am on Oct 6, 2009 (gmt 0)

10+ Year Member

I have a confirmation page on my website that generates a .html file. For example, maybe it's called confirmation.php. Also for example, maybe the page it is trying to generate is called 123456-0987.html. And the folder it is trying to put 123456-0987.html in is called 'receipts'.

so the final location is /home/account_name/public_html/admin/receipts/123456-0987.html

confirmation.html is unable to write to this location. The permissions of the 'receipts' folder are set to 755 and I dont want to change them to 777. How can I get the confirmation.html page to be able to write to the 'receipts' folder without changing the permissions?


10:42 am on Oct 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

why dont you change permissions, upload file and change permissions back to original?

else, you can try to ftp the file to the folder


12:22 pm on Oct 6, 2009 (gmt 0)

WebmasterWorld Administrator mack is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

if you chmod to 777 can the file then be created?



5:24 pm on Oct 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

dougmcc1, this is usually because the php process runs as a different user than the account owner. You mention that the permissions on the relevant directory are 755; that means the only the owner can write to that directory (i.e. the owner's permissions are rwx--read, write, execute--while the permissions for other members of the group and for 'others' are each r-x--read and execute). This means that if the user that php process runs as is any user that is not the account owner or is not a member of the account owner's group, it cannot write to a file with 755 (or even 775) permissions.

To solve this problem, you can basically do one of two things (neither of which will be under your control without root access to the server):

  1. Run php as the account owner (the sever would need some form of suexec enabled to do this) in which case 755 permissions will be sufficient, or
  2. Change the group of the relevant directory to the group of the user the php process runs as in which case you'll need to set permissions to 775

Short answer: contact your host/sysadmin :)

-- b


5:52 pm on Oct 6, 2009 (gmt 0)

10+ Year Member

Excellent info bedlam, thanks so much for that.

Unfortunately, I am the sys admin of my server :) But I do have root access to it, and I do have SuExec enabled.

How do I find out who the owner of the php script is, who the owner of the folder is, and how do I change the owners of the php script or owner or the folder if needed so that they match up?


6:04 pm on Oct 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Well, configuring suexec is probably outside my area of expertise, but for the simple project of making-the-directory-writable-by-php-scripts, you can do this:

  1. Run "chmod 777 directory-name" (where directory-name is the name of your directory)
  2. Write a file to that directory with php
  3. In a shell, run "ls -hal" to find out the group name of the resulting file
  4. Run "chgrp -R group-name directory-name" (where group name is the name you discovered just above)
  5. Run "chmod 775 directory-name" (you may need to do the same on directories inside this one to make them writeable)

-- b


11:26 pm on Oct 6, 2009 (gmt 0)

10+ Year Member

Should I use putty to run the commands you suggested? And be logged in as the root user?


12:28 am on Oct 7, 2009 (gmt 0)

10+ Year Member

Man this was much easier than it was made out to be.

All you have to do for php to be able to write to folders with 755 permissions is put 777 permissions on the folder above it (which allows the script to create folders within this folder), have the php script create the new folder itself that it will be storing files in, then change the folder permissions back to 755 for the folder above it.

Thanks bedlam for pointing me in the right direction!

rahul anand77

5:42 am on Oct 7, 2009 (gmt 0)

5+ Year Member

Yes you can do it. just change the mode to 777 when uploading and after uploading change it to 755 bak using php script. "CHMOD" use this for accomplish you taks.

use this function


7:28 am on Oct 7, 2009 (gmt 0)

10+ Year Member

No need to keep changing the permissions back and forth between 777 and 755. Once the script creates the folder it can write to it with 755 permissions.

Featured Threads

Hot Threads This Week

Hot Threads This Month