Page 1 of 2
fopen(); PHP 5.04
Posted: Tue Jul 19, 2005 2:03 pm
by bokehman
Hi all! I am using PHP 5.04 and having trouble with fopen().
Here's my code:
I want to read from and write to the file but for some reason I can't read from it with either fread() or fgets(). If I use 'r' or 'r+' I can read from it OK but of course when I write to it the old contents has not been deleted.
Posted: Tue Jul 19, 2005 2:07 pm
by Chris Corbyn
Hmmm that sounds incredibly strange. My first though was that read permissions (or execute permissions for the leading path) were not on for PHP but since fopen($bob, 'r'); works that cant be it
Any errors?
EDIT | Ooops, yet another misunderstood question today

Someone find a nice lady to give me a hug

Posted: Tue Jul 19, 2005 2:10 pm
by IceMetalPunk
You can't. w+ and w both truncate (clear) the file upon opening it. Once you write information to it, however, you can read THAT back. I would open the file for reading, read any info you need, then close it and open it for writing.
Or, the better, faster, easier way, use file_get_contents to read all the info before you open it for writing.
-IMP

Posted: Tue Jul 19, 2005 2:37 pm
by bokehman
Here's the problem! I want to lock the file... then read from it... then write to it... and then finally unlock it and release it. If I do a file_get_contents how can I be sure nothing has written to the file between when I read it and when I write to it?
Posted: Tue Jul 19, 2005 3:06 pm
by IceMetalPunk
What do you mean "lock" it? If you call file_get_contents, that reads all the information from it in one line. There should be less than 5 milliseconds between it. If you mean you want to CHMOD it, then allow owner read/write privelege, but lock everyone else's (0700), then afterwards, CHMOD it back to 0777.
-IMP

Posted: Tue Jul 19, 2005 3:11 pm
by Chris Corbyn
Posted: Tue Jul 19, 2005 3:17 pm
by bokehman
What I am saying is I want to lock it (flock) before reading from it so I can be certain that nothing gets written to it between the read and the write. Even if only 5 milliseconds elapses between read and write this is long enough to allow another aplication to access or write to the file.
Posted: Tue Jul 19, 2005 3:20 pm
by Chris Corbyn
Did you read that function above?
If you open the file, lock it, then read it, then write it, then unlock it... voila!
So long as you lock before you read there's no problem... all applications do exactly the same

Posted: Tue Jul 19, 2005 3:28 pm
by bokehman
Not that simple. I want to open the file... create it if it doesn't exist. I need 'w' or 'w+' for this which deletes the content. Once opened I want to lock it (flock)... Then read from it which I can't do if I deleted the contents with 'w+'. Then I want the file emptied... then I want to write to it.
If I use file_get_contents I can't look the file before using it. If I use 'W+' it empties the file before I can read from it. Catch 22
Posted: Tue Jul 19, 2005 3:31 pm
by Chris Corbyn
Example:
I want to open a file and duplicate the last line for example...
Code: Select all
$tmp = array(); //Just for temp storage
$fp = fopen($file, 'r+'); //Open for read/write fp at beginning
flock($fp, LOCK_EX); //Lock it
while (!feof($fp)) { //Not at end of file
$tmp[] = fgets($fp); //Next line onto stack
}
$tmp[] = $tmp[count($tmp)-1]; //Duplicate last line
$data = implode('', $tmp); //Into a string
fwrite($fp, $data); //Write back to file
flock($fp, LOCK_UN); //Unlock it
fclose($fp);
Posted: Tue Jul 19, 2005 3:32 pm
by Chris Corbyn
OK use "a+" if you want to read the data and write to it but also create a new file if it's not there

Posted: Tue Jul 19, 2005 3:35 pm
by bokehman
I can't use 'a+' because after I have read the file I want it cleaned.
Posted: Tue Jul 19, 2005 3:38 pm
by Chris Corbyn
Did you even try it??? clean the file if you want, just put "" back into fwrite().
php.net wrote: Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
EDIT | You'll need fseek() to put the file pointer back to the start after opening it so you can read the data sorry since the pointer is at feof
fseek($fp, 0);
Posted: Tue Jul 19, 2005 3:39 pm
by bokehman
If you try running your script you will find that it doesn't just read the data and then write it back to the file. Every time you run it the file doubles in size. It is duplicating the data.
Edit: Actually it triplicates the data.
Posted: Tue Jul 19, 2005 3:44 pm
by Chris Corbyn
bokehman wrote:If you try running your script you will find that it doesn't just read the data and then write it back to the file. Every time you run it the file doubles in size. It is duplicating the data.

omg... I didn't expect it to work perfectly it was supposed to be pseudo but either way flock() is what you need in combination with fopen($fp, 'a+'); and fseek($fp, 0);
Code: Select all
$fp = fopen($file, 'a+');
flock($fp, LOCK_EX);
fseek($fp, 0); //It's ready for reading now
//Do whatever
fwrite($fp, $anything_here_even_empty);
flock($fp, LOCK_UN);
fclose($fp);