gpt4 book ai didi

php - 如何根据 RFC 2231 在 PHP 中对文件名进行编码?

转载 作者:搜寻专家 更新时间:2023-10-31 20:53:37 25 4
gpt4 key购买 nike

最佳答案

我认为应该这样做:

function rfc2231_encode($name, $value, $charset='', $lang='', $ll=78) {
if (strlen($name) === 0 || preg_match('/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/', $name)) {
// invalid parameter name;
return false;
}
if (strlen($charset) !== 0 && !preg_match('/^[A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*$/', $charset)) {
// invalid charset;
return false;
}
if (strlen($lang) !== 0 && !preg_match('/^[A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*$/', $lang)) {
// invalid language;
return false;
}
$value = "$charset'$lang'".preg_replace_callback('/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/', function($match) { return rawurlencode($match[0]); }, $value);
$nlen = strlen($name);
$vlen = strlen($value);
if (strlen($name) + $vlen > $ll-3) {
$sections = array();
$section = 0;
for ($i=0, $j=0; $i<$vlen; $i+=$j) {
$j = $ll - $nlen - strlen($section) - 4;
$sections[$section++] = substr($value, $i, $j);
}
for ($i=0, $n=$section; $i<$n; $i++) {
$sections[$i] = " $name*$i*=".$sections[$i];
}
return implode(";\r\n", $sections);
} else {
return " $name*=$value";
}
}

请注意,此函数期望输出在一个单独的行中使用,前面有一个适当的换行符(即 CRLF),例如:

"Content-Type: application/x-stuff;\r\n".rfc2231_encode('title', 'This is even more ***fun*** isn\'t it!', 'us-ascii', 'en', 48)

输出是:

Content-Type: application/x-stuff;
title*0*=us-ascii'en'This%20is%20even%20more%20;
title*1=%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it!

另见 Test Cases for HTTP Content-Disposition header field and the Encodings defined in RFC 2047 and RFC 2231/5987 .

关于php - 如何根据 RFC 2231 在 PHP 中对文件名进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4968272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com