fwrite

(PHP 4, PHP 5)

fwriteBinary-safe file write

Description

int fwrite ( resource $handle , string $string [, int $length ] )

fwrite() writes the contents of string to the file stream pointed to by handle.

Parameters

handle

A file system pointer resource that is typically created using fopen().

string

The string that is to be written.

length

If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

Return Values

fwrite() returns the number of bytes written, or FALSE on error.

Notes

Note:

Writing to a network stream may end before the whole string is written. Return value of fwrite() may be checked:

<?php
function fwrite_stream($fp$string) {
    for (
$written 0$written strlen($string); $written += $fwrite) {
        
$fwrite fwrite($fpsubstr($string$written));
        if (
$fwrite === false) {
            return 
$written;
        }
    }
    return 
$written;
}
?>

Note:

On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

Note:

If handle was fopen()ed in append mode, fwrite()s are atomic (unless the size of string exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption.

Note:

If writing twice to the file pointer, then the data will be appended to the end of the file content:

<?php
$fp 
fopen('data.txt''w');
fwrite($fp'1');
fwrite($fp'23');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!
?>

Examples

Example #1 A simple fwrite() example

<?php
$filename 
'test.txt';
$somecontent "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    
// In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    
if (!$handle fopen($filename'a')) {
         echo 
"Cannot open file ($filename)";
         exit;
    }

    
// Write $somecontent to our opened file.
    
if (fwrite($handle$somecontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
    }

    echo 
"Success, wrote ($somecontent) to file ($filename)";

    
fclose($handle);

} else {
    echo 
"The file $filename is not writable";
}
?>

See Also

Коментарии

Автор:
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.
2003-05-19 06:12:36
http://php5.kiev.ua/manual/ru/function.fwrite.html
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));
2003-06-20 05:36:23
http://php5.kiev.ua/manual/ru/function.fwrite.html
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.
2003-08-21 18:04:53
http://php5.kiev.ua/manual/ru/function.fwrite.html
Some people say that when writing to a socket not all of the bytes requested to be written may be written. You may have to call fwrite again to write bytes that were not written the first time. (At least this is how the write() system call in UNIX works.)

This is helpful code (warning: not tested with multi-byte character sets)

function fwrite_with_retry($sock, &$data)
{
    $bytes_to_write = strlen($data);
    $bytes_written = 0;

    while ( $bytes_written < $bytes_to_write )
    {
        if ( $bytes_written == 0 ) {
            $rv = fwrite($sock, $data);
        } else {
            $rv = fwrite($sock, substr($data, $bytes_written));
        }

        if ( $rv === false || $rv == 0 )
            return( $bytes_written == 0 ? false : $bytes_written );

        $bytes_written += $rv;
    }

    return $bytes_written;
}

Call this like so:

    $rv = fwrite_with_retry($sock, $request_string);

    if ( ! $rv )
        die("unable to write request_string to socket");
    if ( $rv != strlen($request_string) )
        die("sort write to socket on writing request_string");
2008-02-20 12:47:02
http://php5.kiev.ua/manual/ru/function.fwrite.html
Автор:
If you write with the pointer in the middle of a file, it overwrites what's there rather than shifting the rest of the file along.
2009-05-15 12:36:07
http://php5.kiev.ua/manual/ru/function.fwrite.html
After having problems with fwrite() returning 0 in cases where one would fully expect a return value of false, I took a look at the source code for php's fwrite() itself. The function will only return false if you pass in invalid arguments. Any other error, just as a broken pipe or closed connection, will result in a return value of less than strlen($string), in most cases 0.

Therefore, looping with repeated calls to fwrite() until the sum of number of bytes written equals the strlen() of the full value or expecting false on error will result in an infinite loop if the connection is lost.

This means the example fwrite_stream() code from the docs, as well as all the "helper" functions posted by others in the comments are all broken. You *must* check for a return value of 0 and either abort immediately or track a maximum number of retries.

Below is the example from the docs. This code is BAD, as a broken pipe will result in fwrite() infinitely looping with a return value of 0. Since the loop only breaks if fwrite() returns false or successfully writes all bytes, an infinite loop will occur on failure.

<?php
// BROKEN function - infinite loop when fwrite() returns 0s
function fwrite_stream($fp$string) {
    for (
$written 0$written strlen($string); $written += $fwrite) {
       
$fwrite fwrite($fpsubstr($string$written));
        if (
$fwrite === false) {
            return 
$written;
        }
    }
    return 
$written;
}
?>
2010-03-24 11:51:46
http://php5.kiev.ua/manual/ru/function.fwrite.html
Автор:
// you want copy dummy file or send dummy file 
// it is possible to send a file larger than 4GB and write without FSEEK used is limited by PHP_INT_MAX. it works on a system 32-bit or 64-bit
// fwrite and fread non pas de limite de position du pointeur 

<?php

$gfz 
filesize_dir("d:\\starwars.mkv"); // 11,5GB
echo 'Z:',$gfz,PHP_EOL;

$fz fopen('d:\\test2.mkv''wb'); 
$fp fopen('d:\\starwars.mkv''rb');
echo 
PHP_EOL;
$a = (float) 0;
while((
$l=fread($fp65536))) { 
   
fwrite($fz$l);
    if((
$a+=65536)%5) echo "\r"'>'$a' : ' $gfz;
}

fclose($fp);
fclose($fz);

// test2.mkv' is 11,5GB

   
function filesize_dir($file) {
       
exec('dir ' $file$inf);
       
$size_raw $inf[6];
       
$size_exp explode(" ",$size_raw);
       
$size_ext $size_exp[19];
       
$size_int = (float) str_replace(chr(255), ''$size_ext);
        return 
$size_int;
    }
?>
2016-04-02 20:48:42
http://php5.kiev.ua/manual/ru/function.fwrite.html
// you want copy dummy file or send dummy file 
// it is possible to send a file larger than 4GB and write without FSEEK used is limited by PHP_INT_MAX. it works on a system 32-bit or 64-bit
// fwrite and fread non pas de limite de position du pointeur 

<?php

$gfz 
filesize_dir("d:\\starwars.mkv"); // 11,5GB
echo 'Z:',$gfz,PHP_EOL;

$fz fopen('d:\\test2.mkv''wb'); 
$fp fopen('d:\\starwars.mkv''rb');
echo 
PHP_EOL;
$a = (float) 0;
while((
$l=fread($fp65536))) { 
   
fwrite($fz$l);
    if((
$a+=65536)%5) echo "\r"'>'$a' : ' $gfz;
}

fclose($fp);
fclose($fz);

// test2.mkv' is 11,5GB

   
function filesize_dir($file) {
       
exec('dir ' $file$inf);
       
$size_raw $inf[6];
       
$size_exp explode(" ",$size_raw);
       
$size_ext $size_exp[19];
       
$size_int = (float) str_replace(chr(255), ''$size_ext);
        return 
$size_int;
    }
?>
2016-04-02 20:49:25
http://php5.kiev.ua/manual/ru/function.fwrite.html
$handles can also be used to output in console like below example

fwrite(STDOUT, "Console Output");
2020-06-16 13:54:37
http://php5.kiev.ua/manual/ru/function.fwrite.html
if you need a function that writes all data, maybe try

<?php

   
/**
     * writes all data or throws
     *
     * @param mixed $handle
     * @param string $data
     * @throws \RuntimeException when fwrite returned <1 but still more data to write
     * @return void
     */
    /*private static*/ 
function fwrite_all($handlestring $data): void
   
{
       
$original_len strlen($data);
        if (
$original_len 0) {
           
$len $original_len;
           
$written_total 0;
            for (;;) {
               
$written_now fwrite($handle$data);
                if (
$written_now === $len) {
                    return;
                }
                if (
$written_now 1) {
                    throw new 
\RuntimeException("could only write {$written_total}/{$original_len} bytes!");
                }
               
$written_total += $written_now;
               
$data substr($data$written_now);
               
$len -= $written_now;
               
// assert($len > 0);
                // assert($len === strlen($data));
           
}
        }
    }
2021-01-28 03:09:32
http://php5.kiev.ua/manual/ru/function.fwrite.html

    Поддержать сайт на родительском проекте КГБ