Page 1 of 1

[Solved] fopen disaster: cant change mode to write files

Posted: Sat Jul 09, 2005 8:24 pm
by angusfighter
i found that i was rejected when trying to use "fopen()" function.
i examined different ways to write files in PHP manual but unsuccessful.
at the moment, the possible reasons due to the problem, i suppose, should be lying on the code below:

===========================================================================

Code: Select all

file_write("xxxx.rss", "we wish you a merry xmsn."); 

function file_write($filename, $content) { 

   if (!is_writable(realpath($filename))) { 
      if (!chmod(realpath($filename), 0666)) { 
         echo "Cannot change the mode of file ($filename)"; 
         exit; 
      } 
   } 

   /* let's omit this coz the program would terminate before executing here */ 
}
=========================================================================

it displays the warning msg: "cannot change the mode of file"
which means, not only i cannot write files
i even cannot "CHMOD"!

could any experts here kindly tell me why it is happening?
really thanks a lot coz i am so frstrated in this what i call "fopen() disaster".

p.s. by the way,
at the same moment, i found my PHP script also cannot use "exec_shell()" too
are the two problems any relation?p;quote;, "we wish you a merry xmsn.&quote;);

function file_write($filename, $content) {

if (!is_writable(realpath($filename))) {
if (!chmod(realpath($filename), 0666)) {
echo &quote;Cannot change the mode of file ($filename)&quote;;
exit;
}
}

/* let's omit this coz the program would terminate before executing here */
}

=========================================================================

it displays the warning msg: "cannot change the mode of file"
which means, not only i cannot write files
i even cannot "CHMOD"!

could any experts here kindly tell me why it is happening?
really thanks a lot coz i am so frstrated in this what i call "fopen() disaster".

p.s. by the way,
at the same moment, i found my PHP script also cannot use "exec_shell()" too
are the two problems any relation?ible reasons due to the problem, i suppose, should be lying on the code below:

===========================================================================

Code: Select all

file_write("xxxx.rss", "we wish you a merry xmsn."); 

function file_write($filename, $content) { 

   if (!is_writable(realpath($filename))) { 
      if (!chmod(realpath($filename), 0666)) { 
         echo "Cannot change the mode of file ($filename)"; 
         exit; 
      } 
   } 

   /* let's omit this coz the program would terminate before executing here */ 
}
=========================================================================

it displays the warning msg: "cannot change the mode of file"
which means, not only i cannot write files
i even cannot "CHMOD"!

could any experts here kindly tell me why it is happening?
really thanks a lot coz i am so frstrated in this what i call "fopen() disaster".

p.s. by the way,
at the same moment, i found my PHP script also cannot use "exec_shell()" too
a
file_write("xxxx.rss", "we wish you a merry xmsn.");

function file_write($filename, $content) {

if (!is_writable(realpath($filename))) {
if (!chmod(realpath($filename), 0666)) {
echo "Cannot change the mode of file ($filename)";
exit;
}
}

/* let's omit this coz the program would terminate before executing here */
}

=========================================================================

it displays the warning msg: "cannot change the mode of file"
which means, not only i cannot write files
i even cannot "CHMOD"!

could any experts here kindly tell me why it is happening?
really thanks a lot coz i am so frstrated in this what i call "fopen() disaster".

p.s. by the way,
at the same moment, i found my PHP script also cannot use "exec_shell()" too
are the two problems any relation?

Posted: Sat Jul 09, 2005 9:26 pm
by ambivalent
Check the file permissions. "apache" probably doesn't have chmod permission on that file (assuming you're using apache, of course).

Posted: Sat Jul 09, 2005 9:34 pm
by angusfighter
hi sir,
this is the permission info that i obtained by some PHP codes:

Code: Select all

-rw-r--r--
but why it still says the file is not writable when i check it by is_writable() ?

Im using apache 1.3.33, php 5.0.4 in Mac OS X Tiger server
and i am just trying to write a local file
whats the problem is it?

Posted: Sat Jul 09, 2005 11:52 pm
by ambivalent
Check the comment section of the manual page for is_writable() - it seems that file permissions can't be changed by a php user under certain circumstances. That's about the best I can do as I'm not on familiar territory when it comes to Macs.

And don't call me sir :)

Posted: Sat Jul 09, 2005 11:59 pm
by angusfighter
thx a lot anyway :wink:

i am trying to figure out if it is not a php problem, but a apache problem.
i guess it should be one of the two problems below:

1. PHP uses an incorrect user to access the file.
if it is true, how do i know what user the running PHP is using?

2. Apache has been config in a way that disallow writing functions.
if it is true, how can i alter this function?

anyway is it already beyond the scope of this forum?

Posted: Sun Jul 10, 2005 12:22 am
by ambivalent
angusfighter wrote:thanks a lot anyway :wink:

1. PHP uses an incorrect user to access the file.
if it is true, how do i know what user the running PHP is using?
Create a file using php/apache, then take a look at the owner and permissions.
2. Apache has been config in a way that disallow writing functions.
if it is true, how can i alter this function?
google it

Posted: Sun Jul 10, 2005 2:21 am
by angusfighter
the problem is i cant create files by PHP...
:cry:

Posted: Sun Jul 10, 2005 3:13 am
by wwwapu
Does "xxxx.rss" file exist? is_writable() returns false if the file does not exist. And chmod() and realpath() can be done only for existing files.

Code: Select all

function file_write($filename, $content) {
  if(file_exists($filename){
    //check permissions and write in file
  }else $openfile=fopen($filename, "w"); //create file and then write it
}
php]function file_write($filename, $content) {
if(file_exists($filename){
//check permissions and write in file
}else $openfile=fopen($filename, "w"); //create file and then write it
}

Posted: Sun Jul 10, 2005 5:16 am
by timvw
you need to make the directory that holds the files writable too...

Posted: Sun Jul 10, 2005 8:00 am
by Chris Corbyn
Use FTP to change the file/folder permissions (PHP doesn't have permission to do it itself).

If FTP doesn't allow CHMOD neither then all is not lost but you would have to use a horrible yucky workaround with mysql.

If there's absolutely no way you can make your folder writable to PHP then store the data in a BLOB field in mysql... there's plenty of tutorial on this but beware, performance can lag.

EDIT | Or you could use the FTP functions of PHP to communicate with localhost

Posted: Sun Jul 10, 2005 6:06 pm
by angusfighter
hi all!
it's a lucky morning
i finally find out the soltions! :idea:
CHEEEEEEEEEEEEEEEERS! :P

what i have done is to alter the "httpd.conf" file of apache
as below:

In "Section 2: 'Main' server configuration"
there is a section originally look like this:

Code: Select all

....

# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use &quote;User nouser&quote; and &quote;Group nogroup&quote;.
#  . On HPUX you may not be able to use shared memory as nobody, and the
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000; 
#  don't use Group &quote;#-1&quote; on these systems!
#
User www
Group www
the user and group permission of apaches is "www"
and what i have done is to change it to my own using ID
that's all!

it spent me TWO days to find out!
sigh~~~~

anyway thx for all who has been helpful here!
with all my heart

angus