homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Unable to dynamically create files on the server using PHP
dougmcc1

10+ Year Member



 
Msg#: 4001911 posted 5:15 am on Oct 6, 2009 (gmt 0)

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?

 

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001911 posted 10:42 am on Oct 6, 2009 (gmt 0)

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

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

mack

WebmasterWorld Administrator mack us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4001911 posted 12:22 pm on Oct 6, 2009 (gmt 0)

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

Mack.

bedlam

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001911 posted 5:24 pm on Oct 6, 2009 (gmt 0)

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

dougmcc1

10+ Year Member



 
Msg#: 4001911 posted 5:52 pm on Oct 6, 2009 (gmt 0)

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?

bedlam

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001911 posted 6:04 pm on Oct 6, 2009 (gmt 0)

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

dougmcc1

10+ Year Member



 
Msg#: 4001911 posted 11:26 pm on Oct 6, 2009 (gmt 0)

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

dougmcc1

10+ Year Member



 
Msg#: 4001911 posted 12:28 am on Oct 7, 2009 (gmt 0)

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+ Year Member



 
Msg#: 4001911 posted 5:42 am on Oct 7, 2009 (gmt 0)

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.
"chmod($filename,MODE(777));"

use this function

dougmcc1

10+ Year Member



 
Msg#: 4001911 posted 7:28 am on Oct 7, 2009 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved