Skip to content

Instantly share code, notes, and snippets.

@sfaut
Created March 6, 2022 16:35
Show Gist options
  • Save sfaut/8a85f8bcf4f1d89aa15582971000a4dd to your computer and use it in GitHub Desktop.
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
<?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