PHP Scripts and Creating Files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hello Everyone:

I have a PHP script that attempts to create a temporary file to be used
during processing.  The script is owned by my username (bryanrme) on
the server.  When the script attempts to create the temporary file, I
get a "failed to open stream: Permission denied" error.  The
permissions on my directory are set to 755.  When I set the permissions
to 757, the creation of the file is successful.  In this case, I notice
that the script spawns a temporary file owned by a user named "99".

Is there any way to avoid this?  I'd prefer not to leave my directory
set at 757, but is there any workaround for this?  I checked my server
configuration and safe mode is not enabled.  Any suggestions?  I would
prefer that the script handle the  creation, usage, and disposable of
the temporary file.


Re: PHP Scripts and Creating Files says...

Quoted text here. Click to load it

(assuming you mean the script is run via a web interface and that this is
a Linux/Apache setup)

PHP runs scripts under the umbrella of the Apache user (looks like this is
user 99 in your setup) with the permissions/etc for that user.  

Set up a user group with both (and only) yourself and the Apache user as
members.  Set the directory's group ownership to this group (leave the
directory's personal ownership as yourself).  Set directory permissions to

Geoff M

Re: PHP Scripts and Creating Files


Thanks for the advice.  Unfortunately, it is not my server.  I purchase
hosting.  I access my settings through the cPanel interface, so I'm not
sure if there is any way to follow the instructions you gave me.  Is
there a way?


Re: PHP Scripts and Creating Files says...

Quoted text here. Click to load it


If your willing to try ....

Use the SSL Shell/Telnet shell access button (near bottom of cPanel page?)
and when you have logged in and have a command line prompt and type in:
groups 99
to see the list of user groups that the user named 99 (which Apache/PHP
appears to be running as) belongs to.

Then type in:
groups <your account name>
and see what groups you are in.

If you're lucky, there might be a group common to you both.  Even if not,
you could still just pick a group that user 99 belongs to (less secure,
but hey, still better than world writable) and you may be able to set the
directory's group ownership to that group.

To see who might be members of a group, type in:
cd /etc/
then type in
more group | grep <group name>

Navigate to one level above the relevant directory:
cd /<path to your directory>/

Typing in:
ls -al
will give you details of directories and files including their user and
group ownership settings.

You can leave the relevant directory personally owned by you and just
change the group ownership by typing:
chgrp <group name> <directory name>

Again type:
ls -al
to check that the change has worked, then type:
chmod 775 <directory name>
and then PHP will be able to write to the directory.


Site Timeline