Skip to content

Instantly share code, notes, and snippets.

@vanleantking
Forked from fj/gist:1597544
Created December 26, 2018 14:07
Show Gist options
  • Save vanleantking/b45ab950d10c4b778a65e59a0627d109 to your computer and use it in GitHub Desktop.
Save vanleantking/b45ab950d10c4b778a65e59a0627d109 to your computer and use it in GitHub Desktop.
Slightly nicer way of writing large files to disk with PHP
// Copy big file from somewhere else
$src_filepath = 'http://example.com/all_the_things.txt'; $src = fopen($src_filepath, 'r');
$tmp_filepath = '...'; $tmp = fopen($tmp_filepath, 'w');
$buffer_size = 1024;
while (!feof($src)) {
$buffer = fread($src, $buffer_size); // Read big file/data source/etc. in small chunks
fwrite($tmp, $buffer); // Write in small chunks
}
fclose($tmp_filepath); // Clean up
fclose($src_filepath);
rename($tmp_filepath, '/final/path/to/file.txt');
@vanleantking
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment