只能访问自己上传的文件

<?php
declare(strict_types=1);
ini_set('display_errors', 'On');
error_reporting(-1);

$uid = (int)($_SESSION['uid'] ?? 0);

// 限定用户访问
if ($uid === 9527) {
    $file = __DIR__ . '/test.png';
    header('Content-Type: image/png');
    readfile($file);
    exit;
}

// 响应403状态码
http_response_code(403);
exit('Forbidden');


//========== 总结 ==========//
// 1、不能直接暴露文件的原始URL。
// 2、通过API向外部提供访问能力。


//========== FAQ ==========//
// 问:浏览器报错“(failed)net::ERR_INCOMPLETE_CHUNKED_ENCODING”
// 答:修改NGINX配置文件,在FASTCGI位置增加如下配置
//     fastcgi_buffer_size           10240k;
//     fastcgi_buffers               16 10240k;
//     fastcgi_busy_buffers_size     20480k;
//     fastcgi_temp_file_write_size  20480k;

Copyright © 2024 码农人生. All Rights Reserved