在 zip 存档中屏蔽文件名的真实(且简单)方法 [英] True(and easy) way to mask filenames in zip archive
本文介绍了在 zip 存档中屏蔽文件名的真实(且简单)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 PHP 中的 ZipArchive 库生成 Zip 存档.它工作得很好,但如果文件名不允许使用符号(例如<">")存档没有这个文件.
I generate Zip archive using ZipArchive lib in PHP. It works good, but if filename have disallowed symbols(for example '<' '>') archive has not this file.
我可以裁剪所有不允许的符号.
I can just crop all dissallowed symbols.
我无缘无故不喜欢庄稼.任何其他方式(可能是掩码,但 '\' 和 '^' 不起作用)?
I motiveless dislike crop. Any another way(may be mask but '\' and '^' doesnt work)?
谢谢.
推荐答案
您可以尝试使用 preg_replace()
为此:
You might try to use preg_replace()
for this:
<?php
header('Content-Type: text/plain');
$file = '!@#$%^&*()ashdgf$%^&*(.pdf';
$file = preg_replace('/[^a-z\.\-\_]/i', '_', $file);
var_dump($file);
?>
节目:
string(26) "__________ashdgf______.pdf"
<小时>
如果 \
和 ^
是可接受的字符,那么使用这个:
If \
and ^
are acceptable charaters, then use this:
<?php
header('Content-Type: text/plain');
$file = 'folder\as!@#$%^&*()ashdgf$%^&*(.zip';
$file = preg_replace('/[^a-z\.\-\_\\\\^]/i', '_', $file);
var_dump($file);
?>
节目:
string(35) "folder\as_____^____ashdgf__^___.zip"
您可以将白名单中的任何其他转义符号添加到此表达式中.
You may add any other escaped symbols from whitelist to this expression.
这篇关于在 zip 存档中屏蔽文件名的真实(且简单)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文