문제

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