[[看到最后的更新和结论,我仍然无法解释为什么]]
所以,我在这里有一个奇怪的案例:
我有一个文件上传表单,用于上传 zip/rar 档案。但是,如果存档中的文件包含特殊字符,则有些会正确显示,而有些则不会。有一个包含字母的文件在上传和移动的 zipfile 中È
变成了一个加号+
,但包含该字母的文件Ö
被正确保留。
上传前的 zip 文件:
Files.zip
Some_file_with_È.pdf
Some_file_with_Î.pdf
Some_file_with_Ñ.pdf
Some_file_with_Ö.pdf
上传和移动后的 zip 文件:
Files.zip
Some_file_with_+.pdf <-- Altered
Some_file_with_+.pdf <-- Altered
Some_file_with_Ñ.pdf
Some_file_with_Ö.pdf
我尝试在 UTF-8 和 ISO-8859-1 / ANSI 中对表单、.php 文件、.js 文件和所有标头进行编码,AddDefaultCharset utf-8
在 Apache 配置中设置,但没有任何改变..
表单 HTML:
<!-- The form. Also tried accept-charset="UTF-8" -->
<form id="uploadzipform" accept-charset="ISO-8859-1">
<input type="file" name="zipfile" id="zipfile">
</form>
<!-- The target iframe to handle fileupload -->
<iframe class="uploadframe" name="zipuploadframe" id="zipuploadframe"></iframe>
jQuery:
$("#uploadzipform").attr(
{
action: "/script/fileupload.php",
method: "POST",
enctype: "multipart/form-data",
target: "zipuploadframe"
});
$("#uploadzipform").submit();
$("#uploadzipform").removeAttr("action method enctype target");
文件上传.php:
/* Irrelevant parts omitted */
$ext = strtolower(pathinfo($_FILES["zipfile"]["name"], PATHINFO_EXTENSION));
// Remove special chars from projectname to make filename
$project = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', substr(utf8_decode($_POST['p']), 0, 9));
$now = date("d-m-Y H.i.s");
$filename = "{$project}_{$now}.{$ext}";
$old = $_FILES["zipfile"]["tmp_name"];
$new = $_SERVER['DOCUMENT_ROOT'] . "/zipfiles/" . $filename;
move_uploaded_file($old, $new);
zip中包含特殊字符的文件怎么可能被更改?
编辑1
忘了说服务器运行的是 Windows Server 2008
编辑2
根据要求,解压缩上传的 zipfile 的代码(使用 7zip 命令行):
$file = str_replace("/", "\\", $_POST['f']); // Path to the zipfile
$path = "C:\\pathtoextract";
mkdir("$path");
$cmd = 'C:\\7-Zip\\7z.exe x "' . $file . '" -aou -o' . $path;
// x = extract
// -aou = append existing files with numbers
// -o = path to extract to
exec($cmd);
更新
我尝试了一些事情,这就是发生的事情:
案例1:公正Ñ
和Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_Ñ.pdf Some_file_with_-.pdf <-- Altered (minus?)
Some_file_with_Ö.pdf Some_file_with_+.pdf <-- Altered
情况2:正常E
和I
+Ñ
和Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_E.pdf Some_file_with_E.pdf <-- Not altered
Some_file_with_I.pdf Some_file_with_I.pdf <-- Not altered
Some_file_with_Ñ.pdf Some_file_with_-.pdf <-- Altered (minus?)
Some_file_with_Ö.pdf Some_file_with_+.pdf <-- Altered
案例 3:正常I
+ È
,Ñ
和Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_È.pdf Some_file_with_+.pdf <-- Altered
Some_file_with_I.pdf Some_file_with_I.pdf <-- Not altered
Some_file_with_Ñ.pdf Some_file_with_-.pdf <-- Altered (minus?)
Some_file_with_Ö.pdf Some_file_with_+.pdf <-- Altered
案例 4: Å
, È
,Ñ
和Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_Å.pdf Some_file_with_+.pdf <-- Altered
Some_file_with_È.pdf Some_file_with_+.pdf <-- Altered
Some_file_with_Ñ.pdf Some_file_with_-.pdf <-- Altered (minus?)
Some_file_with_Ö.pdf Some_file_with_+.pdf <-- Altered
案例 5:同样是第一个档案È
, Î
,Ñ
和Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_È.pdf Some_file_with_+.pdf <-- Altered
Some_file_with_Î.pdf Some_file_with_+.pdf <-- Altered
Some_file_with_Ñ.pdf Some_file_with_-.pdf <-- Altered (minus?)
Some_file_with_Ö.pdf Some_file_with_+.pdf <-- Altered
嗯..现在一切都改变了。
È
案例 6:使用、Î
和创建新Ñ
存档Ö
Before upload: After upload:
Files.zip Files.zip
Some_file_with_È.pdf Some_file_with_È.pdf <-- Not altered
Some_file_with_Î.pdf Some_file_with_Î.pdf <-- Not altered
Some_file_with_Ñ.pdf Some_file_with_Ñ.pdf <-- Not altered
Some_file_with_Ö.pdf Some_file_with_Ö.pdf <-- Not altered
结论:不要编辑压缩文件,每次都创建一个新的......
显然 php 不喜欢修改过的档案,它给出这样的结果仍然很奇怪。