Reply to Re: [PHP] Re: fopen

Your name:

Reply:


Posted by Richard Lynch on 10/04/16 11:11

On Sat, March 19, 2005 6:48 am, John Taylor-Johnston said:
> chmod($defaultfile, 666);

http://php.net/chmod

has examples that tell you exactly why this is wrong...

666 is decimal.

The 666 you want is Octal.

They ain't the same number.

> What does the at_sign mean at the start of this line?
>
> @ $results = fopen($datafilename, "w+");

@ means you are IGNORING any errors this generates.

It's usually a really bad idea unless you have some more code for error
checking.

> if (!$results) { die ("Our results file could not be opened for writing.
> Your score was not recorded. Please contact the person responsible and
> try again later."); }
> flock($results, 2); #lock file for writing
> fwrite($results, $filestr); #write $filestr to $results
> flock($results, 3); #unlock file
> fclose($results); #close file

This is an incorrect way to try to flock a file for writing.

You should:
1) Open the file for READING.
2) flock that file handle, so only YOU have access to that file.
3) Re-open the file for WRITING, now that you have control.
4) Write your data
5) Release the lock.

Your application, as it stands now, has a race condition between the open
for writing and the flock, which sooner or later, WILL bite you in the
ass.

Probably.

Maybe.

If $filestr is small enough, the Linux OS has locking built-in. Windows
may or may not (and I don't care enough about Windows to remember, much
less look it up). FreeBSD and other OSes may or may not also have locking
at OS layer. I wouldn't rely on it, though, since it's trivial to do the
5 steps above.

--
Like Music?
http://l-i-e.com/artists.htm

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация