★ wanayoo — archive 1999 http://uk.php.net/manual/cs/function.fwrite.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<ftruncateglob>
view the version of this page
Last updated: Wed, 20 Aug 2003

fwrite

(PHP 3, PHP 4 )

fwrite -- Binárně bezpečný zápis do souboru

Popis

int fwrite ( int fp, string string [, int length])

fwrite() zapíše obsah řetězce string do souboru specifikovaného pomocí fp. Pokud je zde argument length, zápis skončí po zapsání length bytů nebo při dosažení konce řetězce string, podle toho, co přijde dřív.

Pozn.: pokud je dán argument length, volba magic_quotes_runtime v konfiguraci bude ignorována a nebudou odstraňována žádná lomítka z řetězce string.

Viz také fread(), fopen(), fsockopen(), popen(), a fputs().



add a note add a note User Contributed Notes
fwrite
php at biggerthanthebeatles dot com
21-Aug-2003 08:04
Hope this helps other newbies.

If you are writing data to a txt file on a windows system and need a line break. use \r\n . This will write hex OD OA.

i.e.
$batch_data= "some data... \r\n";
fwrite($fbatch,$batch_data);

The is the equivalent of opening a txt file in notepad pressing enter and the end of the line and saving it.
Andi
17-Jul-2003 07:32
I figured out problems when writing to a file using \r as linebreak, after that file() wasn't able to read the data from that file.
Using \n solved the problem.
chedong at hotmail dot com
20-Jun-2003 07:36
the fwrite output striped the slashes if without length argument given, example:

<?php
$str
= "c:\\01.txt";
$out = fopen("out.txt", "w");
fwrite($out, $str);
fclose($out);
?>

the out.txt will be:
c:^@1.txt
the '\\0' without escape will be '\0' ==> 0x00.

the correct one is change fwrite to:
fwrite($out, $str, strlen($str));
Jake Roberts
04-Jun-2003 04:35
Use caution when using:

$content = fread($fh, filesize($fh)) or die "Error Reading";

This will cause an error if the file you are reading is zero length.

Intead use:

if ( false === fread($fh, filesize($fh)) ) die "Error Reading";

Thus it will be successful on reading zero bytes but detect and error returned as FALSE.
Chris Blown
19-May-2003 08:12
Don't forget to check fwrite returns for errors! Just because you successfully opened a file for write, doesn't always mean you can write to it. 

On some systems this can occur if the filesystem is full, you can still open the file and create the filesystem inode, but the fwrite will fail, resulting in a zero byte file.
seeker at seek dot planet
10-Feb-2003 03:33
[[Editors note: There is no "prepend" mode, you must essentially rewrite the entire file after prepending contents to a string. Perhaps you will use file(), modify, implode(), then fopen()/fwrite() it back]]
To put strings into the front of the file, you need to set place the pointer at the top of the file when openning the file with fopen(), see fopen() for more info.

<ftruncateglob>
 Last updated: Wed, 20 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Thu Nov 6 09:12:56 2003 GMT