Re: using fopen() in write mode is failing



On May 31, 3:59 am, lawrence k <lkrub...@xxxxxxxxxxxxx> wrote:
On May 30, 4:08 pm, cscor...@xxxxxxxxx wrote:



For some reason, I cannot use fopen() on the file in write mode. The
file "time" is in the same directory as the .php file, with
permissions set to 0766.

PHP Version 5.2.5
Apache/2.2.8

code snip in question:

$file = "time";
function updateParseTime($name){
        global $file;
        if (file_exists($name) && file_exists($file)) {
                if(($fp = fopen($file,"wb"))){
                        $lastmod = filemtime($name);
                        if(!(fwrite($fp, $lastmod))){
                                echo "cannot update time";
                        }
                }
                else{
                        echo "$file cannot be opened. \n";
                }
        }
        else{
                echo "$name or $file not found.";
        }

}

on the 4th line it fails the if check ($fp = fopen...) and just
outputs "$file cannot be opened"

Am I missing something?

A little off-topic, but your code is an example of the Arrow Anti-
Pattern:

http://c2.com/cgi/wiki?ArrowAntiPattern

http://www.codinghorror.com/blog/archives/000486.html

I personally find quite a few things some programmers designate as
"anti-patterns" are really just things the particular programmer
doesn't like. For example excessive commenting is claimed to be a
code smell, but I personally would much rather maintain a piece of
code with a lot of comments than a piece of code with none. It's true
enough that comments can be used incorrectly, for example using
esoteric variable names and using comments to say what they are,
variable names should be self-descriptive. And well written code will
get its what and how across quite well, but even the best written code
can't really convey its why. That's where comments come in.

As for the whole "Arrows anti pattern", the author asserts it results
in error handler code a long way from where the error might occur.
This is also true of exceptions, where the handler doesn't even have
to be in the same module. Overuse of exceptions is a far bigger sin
in my opinion than nesting ifs. The code and its error handler are
part of the same section in the arrow approach, and you can tell what
error handle belongs to what code because it will be on the same
indentation level.

Good code should narrative, which is more true of the arrow pattern
than badly used exceptions. There is nothing wrong with the structure
of the OP's code. In fact its structure pointed to exactly where the
problem lay almost immediately and allowed me to formulate a theory
quickly, namely that the script didn't have permission to write to the
file being manipulated.

As for the OP's problem, it looks from the error message PHP emitted
that the PHP user doesn't have write permission for the file in
question. I'd suggest adjusting chown, chmod and chgrp on the file,
but it looks like you've already done all that. Another possibility
is that some other program or script has opened and flocked the file
to prevent more than one process writing to it at a time. If the
software doesn't release its lock no other application or script can
write to it even if they do have permission. If other applications
have been accessing the file in question then try checking that they
have been closed down properly. If they have and the file is still
locked then you may have to resort to a restart of your system.
.



Relevant Pages

  • Re: Web server structure?
    ... I have some php files... ... You can set any permission on any file. ... show what is in the script unless you have the script ... In a common directory you can have programs in several languages, ...
    (alt.html)
  • Re: Web server structure?
    ... I have some php files... ... you think that it's possible to read them instead of executing them a ... You can set any permission on any file. ... what is in the script unless you have the script specifically do so if ...
    (alt.html)
  • Re: PHP (under httpd) reading a file outside DocumentRoot?
    ... I am trying to get a PHP script to read a file that's outside the DocumentRoot of a VirtualHost, in fact well outside the parent directory of all of my virtual hosts. ... This leads me to suspect that the issue is not the permissions themselves, but rather something about my PHP configuration or script (or, possibly, as I noted, my Apache httpd configuration) that is causing PHP to report a permission error on reading the file. ...
    (php.general)
  • Re: Web server structure?
    ... I have some php files... ... You can set any permission on any file. ... show what is in the script unless you have the script specifically do ... Removing r on the directory just prevents ...
    (alt.html)
  • Re: editing licensed shows
    ... You can do almost anything with CHESS if you get permission. ... the first productions done after Broadway was by the Long Beach Civic ... The script was very much changed by director ...
    (rec.arts.theatre.musicals)