Re: using fopen() in write mode is failing
- From: Gordon <gordon.mcvey@xxxxxxxxxxxx>
- Date: Sat, 31 May 2008 04:41:54 -0700 (PDT)
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.
.
- References:
- using fopen() in write mode is failing
- From: cscorley
- Re: using fopen() in write mode is failing
- From: lawrence k
- using fopen() in write mode is failing
- Prev by Date: Re: able to upload 28 meg file, yet php.ini limits posts to 10 megs. What is up with that?
- Next by Date: Re: Interactive mode not very useful
- Previous by thread: Re: using fopen() in write mode is failing
- Next by thread: Re: using fopen() in write mode is failing
- Index(es):
Relevant Pages
|