By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

fwrite() returns false after a successful fopen($file, 'a+b')

P: n/a


I wrote the function below as part of a larger class.

The fopen stage works, and, as according to the documentation at
www.php.net/fopen that succesfully creates a new file.

The fwrite stage returns false, however, on the first time the function is
run, but returns true the second time it is run.

Can anyone suggest why fwrite only works the second time?

There are no entries in the apache log, even with error reporting fully
turned up (note that I have removed @ from infront of each function, so
would expect something).
---

function writeDataToFile ($data, $file) {

# Attempt to open the file in read+write mode
if (!$fileHandle = fopen ($file, 'a+b')) {return false;} else {

# Attempt to write the data
#!# For some reason this returns false just after the first time trying to create the file
if (!fwrite ($fileHandle, $data)) {return false;} else {

# Having written to the file, close it
fclose ($fileHandle);

# Return a positive result
return true;
}
}
}

---

Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Martin Lucas-Smith wrote:

I wrote the function below as part of a larger class.

The fopen stage works, and, as according to the documentation at
www.php.net/fopen that succesfully creates a new file.

The fwrite stage returns false, however, on the first time the function is
run, but returns true the second time it is run.

Can anyone suggest why fwrite only works the second time?

There are no entries in the apache log, even with error reporting fully
turned up (note that I have removed @ from infront of each function, so
would expect something).

---

function writeDataToFile ($data, $file) {

# Attempt to open the file in read+write mode
if (!$fileHandle = fopen ($file, 'a+b')) {
return false;
}
else {

# Attempt to write the data
#!# For some reason this returns false just after the first time trying to create the file
if (!fwrite ($fileHandle, $data)) { fclose($fileHandle);
//^^^^^^^^^^^^^^^^^^^^ Never return without closing the file first return false;
}
else {

# Having written to the file, close it
fclose ($fileHandle);

# Return a positive result
return true;
}
}
}


Not to start a flame war, but I've rewritten the code in the form that I prefer,
but using your code. My personal preference would be to remove the ELSEs when
the IF that it mates with includes a RETURN.

I don't see anything wrong with the code, except you should have an fclose in
the fwrite fail block. That may or may not be part of the problem.

This is a binary file you're trying to append to? If not, remove the "b".
You're sure that the fopen is working (nothing in the code, as you've posted it,
differentiates between fopen failing and fwrite failing). What code are you
using to call the function? Is it called twice in one script or are you running
the script twice? What operating system is this running under?

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.