简体   繁体   中英

How can I replace white space in filename of uploaded file

I'm making a SWF uploader and have my HTML form done.

It works totally fine until I upload a SWF file with spaces in the name.

How can I replace whitespace with underscores?

I have tried...

str_replace(" ","_", $file);

...and...

preg_replace(" ","_", $file);

How can I replace whitespace with underscores?

The \\s character class will match whitespace characters. I've added the + quantifier to collapse multiple whitespace to one _ . If you don't want that, remove the + .

$file = preg_replace('/\s+/', '_', $file);

该函数是正确的,但您必须将其分配给变量。

$filename = str_replace/preg_replace(" ","_", $file);

I usually approach it from the other side and only allow characters from a white-list; I replace everything except these characters:

$file = preg_replace("/[^-_a-z0-9]+/i", "_", $file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM