Created
March 6, 2022 16:35
-
-
Save sfaut/8a85f8bcf4f1d89aa15582971000a4dd to your computer and use it in GitHub Desktop.
PHP stream_write_to_file() has been inspired by standard stream_copy_to_stream() which accepts only streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('stream_write_fo_file')) { | |
/** | |
* Write a stream to a file, old file contents is lost | |
* | |
* Parameters: | |
* $from: the source stream | |
* $to: the destination file | |
* $length: maximum bytes to copy, but default all bytes left are copied | |
* $offset: the offset where to start to copy data | |
* | |
* Returns the total bytes copied, or false on failure | |
*/ | |
function stream_write_to_file($from, string $to, int $length = null, int $offset = 0) //: int|false | |
{ | |
$to_stream = fopen($to, 'w'); | |
if ($to_stream === false) { | |
return false; | |
} | |
$result = stream_copy_to_stream($from, $to_stream, $length, $offset); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment