Page 1 of 1
Doubts regarding change of file access mode...
Posted: Mon Jan 30, 2006 3:49 pm
by raghavan20
If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
Re: Doubts regarding change of file access mode...
Posted: Mon Jan 30, 2006 4:43 pm
by Chris Corbyn
raghavan20 wrote:If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
If you opened the file in readonly mode then you'd have to close and reopen it... this is beyond the control of PHP and is to do with how the filesystem works

The handle created when you open a file in PHP is nothing more than a pointer to an open file.
Re: Doubts regarding change of file access mode...
Posted: Mon Jan 30, 2006 5:58 pm
by raghavan20
d11wtq wrote:raghavan20 wrote:If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
If you opened the file in readonly mode then you'd have to close and reopen it... this is beyond the control of PHP and is to do with how the filesystem works

The handle created when you open a file in PHP is nothing more than a pointer to an open file.
say if we change the mode again using fopen again and assign it to the same pointer with had access to the file in different mode...then is the older reference killed or do I have to unset and assign again?
Posted: Mon Jan 30, 2006 10:28 pm
by josh
The old file will remain open your pointer to it will just have been overwritten. Use fclose() whenever you are done with a file (all handles are explicitly closed at the end of the script, but having more files open at one time then you need is a resource hog)
Posted: Tue Jan 31, 2006 5:12 am
by raghavan20
I will make the question still more clearer with the code...
Code: Select all
$fp = fopen("trial.txt", "w+");
$fp = fopen("trial.txt", "a+");
Now will the second line kill the reference, $fp in the first line or do I have to use something like this...?
Code: Select all
$fp = fopen("trial.txt", "w+");
unset($fp);
$fp = fopen("trial.txt", "a+");
Posted: Tue Jan 31, 2006 5:35 am
by Jenk
Code: Select all
<?php
$fp = fopen('file.txt', 'a+');
fclose($fp);
$fp = fopen('file.txt', 'w');
//etc..
?>
Just as was described by jshpro2.. $fp is simply a callsign for the filepointer generated by fopen()
assigning a new value to $fp, be it another pointer or something completely different will not close the filepointer generated by fopen, fclose does this.
However, your question is different, but moot due to the above example bein the "best practice" of closing a filepointer when you have finished with it..
Code: Select all
<?php
$fp = fopen('file', 'a+');
//do stuff...
$fp = fopen('file', 'w');
$frwite ($fp, 'val'); //will write to the file 'file' .. the original filepointer generated for read-only is now 'lost' and is floating around in memory..
Posted: Tue Jan 31, 2006 5:57 am
by raghavan20
That clears off my doubt Jenk and jshpro....so I have to close explicitly or the memory is not deallocated...
Posted: Tue Jan 31, 2006 6:05 am
by Jenk
Yes
