Cant seem to write to file

Discussions around the use of PHP on VMS
Post Reply
Message
Author
User avatar
beaudoin_p
$ HELP
Posts: 13
Joined: Fri Oct 21, 2005 10:31 am
Location: London UK

Cant seem to write to file

#1 Post by beaudoin_p »

Gents,
Me again ...

I have managed to open a file and write one line to it. Unfortunately I ami trying to write multiple lines to it. To shorten the explaination, I offer the following code extract followed by comments:

$handle = fopen($ufname,"w");

///File opens OK

Later on in the script...

if ($HdrCnt >= 1)
{
echo 'Writing header1 '.$header['Hrd1'];
fwrite($handle,$header['Hdr1']."\n");
}
/// This writes as expected

///Still later ...

while ($row = mysql_fetch_array($Data))
{
$buffer = implode(",",$row);
$DataLen = strlen($buffer);
echo 'Writing Buffer = '.$buffer;
$DatW = fwrite($handle,$buffer,$DataLen);
echo 'DataLen = '.$DataLen.', WriteLen = '.$DatW;
}

This produces a correct buffer (on the 1st echo) , no write into the file and on the second echo confirms correct len was passed and 0 bytes written.

I have run out of ideas as to why this is failing. Any hints are gratefully acknowleged.

BTW. This exports, from MYSQL a previously IMPORTED excel spreadsheet. If anyone is interested in this code, please send me an email (paul.beaudoin@hsbc.com) for some explanation and/or source.

Many thanks

Paul

User avatar
beaudoin_p
$ HELP
Posts: 13
Joined: Fri Oct 21, 2005 10:31 am
Location: London UK

#2 Post by beaudoin_p »

Gents,

I know it is bad form to answer yourself ( and indeed somewhat suspect to be talking to one's self...) But as I have a partial answer ...

There seems to be a bug (or undocumented behavoiur) with fwrite in as much as when a trailing '\n' is not present in the buffer, it fails to write. After some testing, it appears that somewhere on the PHP/VMS border if there is no linefeed char (denoting end of line here), the line refuses to write and it (correctly) reports 0 chars written with no further errors (or hints). To me this is unexpected - does any one else agree?

Thanks and regards

Paul

User avatar
kenrbnsn
Posts: 4
Joined: Thu Apr 21, 2005 8:31 pm
Location: Hillsborough, NJ USA
Contact:

#3 Post by kenrbnsn »

I have seen this happen on the Windows PHP also. Just get into the habit of always appending a new-line character to all your writes.

Ken

Post Reply