微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在尝试使用 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.

原文地址:https://www.jb51.cc/php/136137.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐