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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...