Created
August 30, 2016 12:02
-
-
Save m3m0r7/91c587b4f1d15068df25f6b21d962d10 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
define('READ_SIZE', 2 ** 24); | |
if (isset($argv[1], $argv[2])) { | |
$from = $argv[1]; | |
$to = $argv[2]; | |
if (!is_file($from)) { | |
printf("This is directory or not found file.\n"); | |
return; | |
} | |
if (is_dir($to)) { | |
$to .= '/' . basename($from); | |
} | |
if (is_file($to)) { | |
do { | |
printf("Overwrite file ? (y/n): "); | |
$answer = fread(STDIN, 1); | |
if ($answer === 'n') { | |
printf("Bye.\n"); | |
return; | |
} else if ($answer !== 'y') { | |
printf("Missing answer. "); | |
} | |
} while ($answer !== 'y'); | |
} | |
// start copy | |
$handle = fopen($from, 'r'); | |
$newHandle = fopen($to, 'wb'); | |
$size = filesize($from); | |
$read = 0; | |
$beforeStrings = ''; | |
printf("Start Copy\n"); | |
while (!feof($handle) && $read < $size) { | |
$buffer = fread($handle, READ_SIZE); | |
fwrite($newHandle, $buffer); | |
$read += READ_SIZE; | |
$result = floor(($read / $size) * 100); | |
for ($i = 0; $i < strlen($beforeStrings); $i++) echo chr(0x08); | |
$strings = sprintf("copied (%d%%/100%%)", $result); | |
if ($beforeStrings !== $strings) { | |
print($strings); | |
} | |
$beforeStrings = $strings; | |
} | |
printf("\n"); | |
} else { | |
printf("no parameters.\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment