加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – file_get_contents – 下载文件名中带空格的文件无法正常

发布时间:2020-12-13 13:03:24 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用 file_get_contents() 功能下载文件. 但是,如果文件的位置是http://www.example.com/some name.jpg,则该功能无法下载. 但是,如果URL是http://www.example.com/some name.jpg,则会下载相同的URL. 我尝试了 rawurlencode() ,但这会覆盖URL中的所
我正在尝试使用 file_get_contents()功能下载文件.
但是,如果文件的位置是http://www.example.com/some name.jpg,则该功能无法下载.

但是,如果URL是http://www.example.com/some name.jpg,则会下载相同的URL.

我尝试了rawurlencode(),但这会覆盖URL中的所有字符,下载再次失败.

有人可以为此建议一个解决方案吗?

我认为这对你有用:
function file_url($url){
  $parts = parse_url($url);
  $path_parts = array_map('rawurldecode',explode('/',$parts['path']));

  return
    $parts['scheme'] . '://' .
    $parts['host'] .
    implode('/',array_map('rawurlencode',$path_parts))
  ;
}


echo file_url("http://example.com/foo/bar bof/some file.jpg") . "n";
echo file_url("http://example.com/foo/bar+bof/some+file.jpg") . "n";
echo file_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "n";

产量

http://example.com/foo/bar%20bof/some%20file.jpg
http://example.com/foo/bar%2Bbof/some%2Bfile.jpg
http://example.com/foo/bar%20bof/some%20file.jpg

注意:

我可能会使用urldecode和urlencode,因为每个url的输出都是相同的. rawurlencode将保留偶数,可能适用于您正在使用的任何URL.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读