Created
January 7, 2020 12:33
-
-
Save valmayaki/cb74fffb4a0bc960b3d2e3f9db78b423 to your computer and use it in GitHub Desktop.
Download files in PHP on the server
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 | |
| function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { | |
| static $filesize = null; | |
| switch ($notification_code) { | |
| case STREAM_NOTIFY_RESOLVE: | |
| case STREAM_NOTIFY_AUTH_REQUIRED: | |
| case STREAM_NOTIFY_COMPLETED: | |
| case STREAM_NOTIFY_FAILURE: | |
| case STREAM_NOTIFY_AUTH_RESULT: | |
| /* Ignore */ | |
| printf("Current status: %s \n and size : %d kb", $message, $bytes_transferred/1024); | |
| break; | |
| case STREAM_NOTIFY_REDIRECTED: | |
| echo "Being redirected to: ", $message, "\n"; | |
| break; | |
| case STREAM_NOTIFY_CONNECT: | |
| echo "Connected...\n"; | |
| break; | |
| case STREAM_NOTIFY_FILE_SIZE_IS: | |
| $filesize = abs($bytes_max); | |
| echo "Filesize: ", $filesize, "\n"; | |
| break; | |
| case STREAM_NOTIFY_MIME_TYPE_IS: | |
| echo "Mime-type: ", $message, "\n"; | |
| break; | |
| case STREAM_NOTIFY_PROGRESS: | |
| if ($bytes_transferred > 0 ) { | |
| // if ($bytes_transferred > 0 && $filesize >= 8192) { | |
| // $bytes_transferred += 8192; | |
| if (!isset($filesize)) { | |
| printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024); | |
| } else { | |
| $length = (int)(($bytes_transferred/$filesize)*100); | |
| // if (!$length || $length < 1){ | |
| // printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024); | |
| // }else{ | |
| printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024); | |
| // } | |
| } | |
| } | |
| break; | |
| } | |
| ob_flush(); | |
| } | |
| function downloadFile($path, $fileName = null){ | |
| if (empty($fileName)){ | |
| $fileName = sys_get_temp_dir().'/'.static::getFileNameFromUrl($path); | |
| } | |
| $ctx = stream_context_create(); | |
| stream_context_set_params($ctx, array("notification" => "stream_notification_callback")); | |
| $fin = fopen($path, "rb", false, $ctx); | |
| $fout = fopen($fileName, 'w'); | |
| $buffer_size = 4096; | |
| // file_put_contents($fileName, $fp); | |
| while(!feof($fin)){ | |
| $data = fread($fin, $buffer_size); | |
| fwrite($fout, $data, strlen($data)); | |
| } | |
| fclose($fin); | |
| fclose($fout); | |
| } | |
| function getFileNameFromUrl($url){ | |
| $name = ""; | |
| $path = parse_url($url, PHP_URL_PATH); | |
| $pathInfo = pathinfo($path); | |
| $name .= $pathInfo['filename']; | |
| if (isset($pathInfo['extension']) && !empty($pathInfo['extension']) ){ | |
| $name .= ".".$pathInfo['extension']; | |
| } | |
| return $name; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment