get_headers()获取文件大小,解决https链接报错问题

获取文件大小代码

$file_size = @get_headers($file_path, 1);
$file_size = $file_size['Content-Length'] ?? 0;

获取https的链接时报错日志

error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
[ error ] [2]get_headers(): Failed to enable crypto
[ error ] [2]get_headers(https://wvideo.haier.com/746efc9-17de531009e.mp4): failed to open stream: operation failed
[ error ] [2]get_headers(): SSL operation failed with code 1. OpenSSL Error messages:

解决方法

// 关闭https验证,不然get_headers不起作用
stream_context_set_default([
'ssl' => [
        'verify_host'      => false,
        'verify_peer'      => false,
        'verify_peer_name' => false,
    ],
]);
$file_size = @get_headers($file_path, 1);
$file_size = $file_size['Content-Length'] ?? 0;
上一篇:JDBC高级之数据库连接池


下一篇:学习前端web的心路历程-----web服务器软件Tomcat以及Servlet入门