fopen(); PHP 5.04

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

fopen(); PHP 5.04

Post by bokehman »

Hi all! I am using PHP 5.04 and having trouble with fopen().
Here's my code:

Code: Select all

$fp = fopen("users.php", "w+");
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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :oops: Someone find a nice lady to give me a hug :(
Last edited by Chris Corbyn on Tue Jul 19, 2005 2:31 pm, edited 1 time in total.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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?
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I can't use 'a+' because after I have read the file I want it cleaned.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
Last edited by Chris Corbyn on Tue Jul 19, 2005 3:40 pm, edited 1 time in total.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Last edited by bokehman on Tue Jul 19, 2005 3:46 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
8O 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);
Post Reply