Permission denied on fopen php function on linux

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

Permission denied on fopen php function on linux

Post by hardik »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Permission denied to create new pdf file using fopen php function in "wb" mode.

i am using dotproject 2.0.4 on fc3.
it is working fine.
but i want to generate pdf file report during this time i face permission denied problem.

Warning: fopen(/var/www/html/dotproject/files/temp/temp1.pdf): failed to open stream: Permission denied in /var/www/html/dotproject/modules/projects/reports/tasklogs.php on line 307
Could not open file to save PDF. 

when i run below test php file using command line it will create file at particular location.

Code: Select all

<?php
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
print $file;
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
print "hai $file1";
?>
but when i run this php file in web browser it would not create file.

as i conclude it is problem with related to permission for user.

bec when i run from command line it runs with the permissions of the user who started the script means root if i login as root user.

but from web browser it runs as web browser user.

in my httpd.conf file user and group is as follow

User Apache
Group Apache


how to set permission so web browser user also can access file and create file.

so can any one help to resolve permission problem

it resolves my many issue related to permission.

thanks
:(


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Change the permissions of the folder you are writing to, or have that folder actually be created by your scripts with mkdir() so that the script is considered the directory owner.
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

AOL Speak

Post by hardik »

here
i have /var/www/html/dotproject/files/temp/ directory already.

i am not use mkdir function to create that.


i already chnge mode of directory temp to 777 using

chmod 777 temp command

but still it is not working

do [s]u[/s] you have any idea what's going wrong

thanks for your reply
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

What directory are you running the script from? Maybe you should use a path relative to the actual file.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

<?php
function foo($x) {
	$path = '';
	foreach(explode('/', dirname($x)) as $p) {	
		$path .= $p.'/';
		echo 
			is_dir($path) ? 'd':'-',
			is_readable($path) ? 'r':'-',
			is_writable($path) ? 'w':'-',
			is_executable($path) ? 'x':'-',
			' ', $path, "<br />\n";
	}
}

foo("/var/www/html/dotproject/files/temp/test.txt");
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
print $file;
foo("/var/www/html/dotproject/files/temp/test1.pdf");
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
print "hai $file1";
?>
and post the output
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

YOU need to change the permission for the temp directory to 0777.

The reason it is working from command line is because the user for the command line is the same as the user permission for the temp directory. When it is failing from the browser execution it is because Apache has a DIFFERENT user that is executing the program and that user does NOT HAVE PERMISSION to write to the temp directory. So either change the temp directory owner to the same user as Apache or change the permissions to 0777.
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

Post by hardik »

Code: Select all

<?php
function foo($x) {
        $path = '';
        foreach(explode('/', dirname($x)) as $p) {     
                $path .= $p.'/';
                echo
                        is_dir($path) ? 'd':'-',
                        is_readable($path) ? 'r':'-',
                        is_writable($path) ? 'w':'-',
                        is_executable($path) ? 'x':'-',
                        ' ', $path, "<br />\n";
        }
}

foo("/var/www/html/dotproject/files/temp/test.txt");
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
print $file;
foo("/var/www/html/dotproject/files/temp/test1.pdf");
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
print "hai $file1";
?>
Here is the output of Above Code :

Code: Select all

dr-x /
d--x /var/
dr-x /var/www/
dr-x /var/www/html/
dr-x /var/www/html/dotproject/
dr-x /var/www/html/dotproject/files/
dr-x /var/www/html/dotproject/files/temp/
dr-x /
d--x /var/
dr-x /var/www/
dr-x /var/www/html/
dr-x /var/www/html/dotproject/
dr-x /var/www/html/dotproject/files/
dr-x /var/www/html/dotproject/files/temp/
hai
so you can see here it does not display file descriptor and also not create file at particular location.
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

Post by hardik »

yes you are correct.

when we run that test code from browser. it runs as different user then command line user.

i have change permission of temp file to 777.

on other side also try with change group user and owner also with

chgrp and chown command to apache. but still it is not working.

so is there any idea to solve it.

thanks for your help
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

Post by hardik »

how to give read/write permission to apache user so it can create file or directory in php through web browser.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

chmod
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_erros', true);

function print_stat($path) {
	$s = @stat($path);

	if ( false===$s) {
		echo "cannot stat $path\n";
		return false;
	}
	
	$m = $s['mode'];
	echo $m & 0040000 ? 'd':'-';
	
	for($i=6; $i>=0; $i-=3) {
		echo $m & (1<<($i+2)) ? 'r' : '-';
		echo $m & (1<<($i+1)) ? 'w' : '-';
		echo $m & (1<<($i+0)) ? 'x' : '-';
	}
	
	echo
		' uid:', str_pad($s['uid'], 4, ' ', STR_PAD_RIGHT),
		' gid:', str_pad($s['gid'], 4, ' ', STR_PAD_RIGHT),
		' ', $path, "\n";
	return true;
}

function foo($path) {
	$p = '';
	foreach(explode('/', dirname($path)) as $d) {
		$p .= $d.'/';
		print_stat( $p );
	}
	print_stat( $path );
}

echo "<pre>\n";
foo("/var/www/html/dotproject/files/temp/test.txt");
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
echo $file, "\n";
foo("/var/www/html/dotproject/files/temp/test1.pdf");
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
echo "hai $file1", "\n";
echo "</pre>\n";
and post the output (I don't think the values are secret)
hardik
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 2:48 am

Post by hardik »

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_erros', true);

function print_stat($path) {
        $s = @stat($path);

        if ( false===$s) {
                echo "cannot stat $path\n";
                return false;
        }
       
        $m = $s['mode'];
        echo $m & 0040000 ? 'd':'-';
       
        for($i=6; $i>=0; $i-=3) {
                echo $m & (1<<($i+2)) ? 'r' : '-';
                echo $m & (1<<($i+1)) ? 'w' : '-';
                echo $m & (1<<($i+0)) ? 'x' : '-';
        }
       
        echo
                ' uid:', str_pad($s['uid'], 4, ' ', STR_PAD_RIGHT),
                ' gid:', str_pad($s['gid'], 4, ' ', STR_PAD_RIGHT),
                ' ', $path, "\n";
        return true;
}

function foo($path) {
        $p = '';
        foreach(explode('/', dirname($path)) as $d) {
                $p .= $d.'/';
                print_stat( $p );
        }
        print_stat( $path );
}

echo "<pre>\n";
foo("/var/www/html/dotproject/files/temp/test.txt");
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
echo $file, "\n";
foo("/var/www/html/dotproject/files/temp/test1.pdf");
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
echo "hai $file1", "\n";
echo "</pre>\n";
here is the output

Code: Select all

drwxr-xr-x uid:0    gid:0    /
drwxrwxrwx uid:0    gid:0    /var/
drwxrwxrwx uid:0    gid:0    /var/www/
drwxrwxrwx uid:0    gid:0    /var/www/html/
drwxrwxrwx uid:500  gid:500  /var/www/html/dotproject/
drwxrwxr-x uid:0    gid:0    /var/www/html/dotproject/files/
drwxrwxrwx uid:0    gid:0    /var/www/html/dotproject/files/temp/
cannot stat /var/www/html/dotproject/files/temp/test.txt

drwxr-xr-x uid:0    gid:0    /
drwxrwxrwx uid:0    gid:0    /var/
drwxrwxrwx uid:0    gid:0    /var/www/
drwxrwxrwx uid:0    gid:0    /var/www/html/
drwxrwxrwx uid:500  gid:500  /var/www/html/dotproject/
drwxrwxr-x uid:0    gid:0    /var/www/html/dotproject/files/
drwxrwxrwx uid:0    gid:0    /var/www/html/dotproject/files/temp/
cannot stat /var/www/html/dotproject/files/temp/test1.pdf
hai
still not able to create directory.

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hardik wrote:still not able to create directory.
Printing some values will not fix the problem. It only helps to find the problem ...maybe

I forgot a line in the script, sorry

Code: Select all

echo "<pre>\n";

echo 'id: ', system('id'); echo "<br />\n"; // this should print your effective uid and gid(s)

foo("/var/www/html/dotproject/files/temp/test.txt");
$file = fopen("/var/www/html/dotproject/files/temp/test.txt","w");
echo $file, "\n";
foo("/var/www/html/dotproject/files/temp/test1.pdf");
$file1 = fopen("/var/www/html/dotproject/files/temp/test1.pdf","wb");
echo "hai $file1", "\n";
echo "</pre>\n";
Post Reply