473,408 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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



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
1 6530
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
10
by: cs | last post by:
if i have typedef struct{ unsigned a; unsigned *b; }tp; i would like to ask if it is ok for the "C standard 89" doing something like this {
1
by: GeorgeLost | last post by:
I have been learnig php over the past few months so I'm still new at this. I want to import an Excel csv file into mysql database using php. After trying countless online scripts, I've discovered...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
8
by: zaheer031 | last post by:
I am using the following code typedef struct A { int x; int y; int z; }; int main(void) {
4
anfetienne
by: anfetienne | last post by:
hi i've got this code that writes data to a text file. When i test it with a standard array eg array(1,2,3) it works perfectly fine but when i try it with $_POST i get the error message Warning:...
7
by: chazzy69 | last post by:
Ok basically i need to figure out why i cant pass a variable as a parmater in the fwrite function. heres the code that does NOT work - $output = "hello world"; //note not the actual string...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.