Page 1 of 1
Permission denied on fopen php function on linux
Posted: Fri Jun 29, 2007 3:26 am
by hardik
feyd | Please use 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
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]
Posted: Fri Jun 29, 2007 4:30 am
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.
AOL Speak
Posted: Fri Jun 29, 2007 5:20 am
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.
Posted: Fri Jun 29, 2007 10:06 am
by superdezign
What directory are you running the script from? Maybe you should use a path relative to the actual file.
Posted: Fri Jun 29, 2007 10:19 am
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
Posted: Fri Jun 29, 2007 12:35 pm
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.
Posted: Fri Jun 29, 2007 11:30 pm
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.
Posted: Fri Jun 29, 2007 11:44 pm
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
Posted: Sat Jun 30, 2007 12:35 am
by hardik
how to give read/write permission to apache user so it can create file or directory in php through web browser.
Posted: Sat Jun 30, 2007 12:37 am
by Benjamin
chmod
Posted: Sat Jun 30, 2007 1:09 am
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)
Posted: Sat Jun 30, 2007 2:54 am
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
Posted: Sat Jun 30, 2007 7:38 am
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";