I have the following script to write a file that i grabbed from online.
$newfname = $data['transferPath'] . '/' . $data['filename'];
$file = fopen ($data['filePath'], "rb");
if(!$file) {
throw new Exception('Unable to open file for reading ' . $file);
}
if($file) {
$newf = fopen ($newfname, "wb");
if(!$newf) {
throw new Exception("Cant open file for writing");
}
if($newf) {
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
}
if($file) {
fclose($file);
}
if($newf) {
fclose($newf);
}
when i post the data and run this script I keep getting the exception for cant open file for writing because there already is a file but that name in the same directory. Im trying to overwrite the file with the new file any ideas what i can do. iv tried using the options for fopen using w and w+. I need to completely overwrite the file there if exists otherwise create the file.
Try using the a+
ending parameter to the fopen()
function.
Look here for a complete list of the possible parameters and what they do to find the right one for your purpose!
If none of these satisfy your needs then you could do something like this:
$fopen($data['filePath'], "r");
if($file){
unlink($data['filePath']);
}
Then after it deletes the file, if it's there, then just do another fopen
with the w
parameter.