質問

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)?

Thanks.

役に立ちましたか?

解決

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);
?>

Shows:

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);
?>

Shows:

string(35) "folder\as_____^____ashdgf__^___.zip"

You may add any other escaped symbols from whitelist to this expression.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top