public static function DownloadFile()
 {
     $id = intval(Input::get('file_id'));
     $fileInfo = File::whereId($id)->whereBanned(false)->first();
     if ($fileInfo == null) {
         abort(404);
     }
     $config = Config::whereKey('file_dir')->whereApplied(true)->first();
     $sourceFile = $config->value . $fileInfo->file_path;
     $outFile = $fileInfo->filename;
     if (!is_file($sourceFile)) {
         abort(404);
     }
     LogController::LogDownload($id);
     $len = filesize($sourceFile);
     //获取文件大小
     $filename = basename($sourceFile);
     //获取文件名字
     $ctype = FileDownloadController::GetFileExtension($outFile);
     //echo $outFile;
     header("Cache-Control: public");
     header("Content-Type: {$ctype}");
     header("Content-Disposition: attachment; filename=" . $outFile);
     header("Accept-Ranges: bytes");
     $size = filesize($sourceFile);
     $range = 0;
     if (isset($_SERVER['HTTP_RANGE'])) {
         list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
         str_replace($range, "-", $range);
         $size2 = $size - 1;
         //文件总字节数
         $new_length = $size2 - $range;
         header("HTTP/1.1 206 Partial Content");
         header("Content-Length: {$new_length}");
         header("Content-Range: bytes {$range}{$size2}/{$size}");
     } else {
         $size2 = $size - 1;
         header("Content-Range: bytes 0-{$size2}/{$size}");
         header("Content-Length: " . $size);
     }
     $fp = fopen("{$sourceFile}", "rb");
     fseek($fp, $range);
     while (!feof($fp)) {
         set_time_limit(0);
         print fread($fp, 1024 * 8);
         flush();
         ob_flush();
     }
     fclose($fp);
     exit;
 }
Example #2
0
});
Route::get('view', function () {
    $file = \App\Http\Controllers\FileDownloadController::GetFileInfoByID(\Illuminate\Support\Facades\Input::get('file_id'));
    if ($file == null) {
        abort(404);
    }
    return view('detail')->with(['file' => $file, 'title' => '资源详情-' . $file->filename]);
});
Route::get('test', function () {
    $str = "\n    <html>\n    <body>\n    <a href='download?file_id=1'>测试</a>\n</body>\n    </html>\n    ";
    return $str;
});
Route::get('all-files', function () {
    return \App\Http\Controllers\FileDownloadController::GetAllFiles();
});
Route::get('download', function () {
    \App\Http\Controllers\FileDownloadController::DownloadFile();
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
    //
});