Phineas_0510
Phineas_0510

Reputation: 43

PHP Upload - Spaces in File Name

When I use my upload script to upload a PHP file, I can't upload a file with spaces in it (I get a 500 error). Is there a way so my code automatically puts an underscore in the file name instead of the space? All help is greatly appreciated. :)

Upvotes: 1

Views: 4348

Answers (2)

Priyank
Priyank

Reputation: 3868

Use this regular expression.suppose your filename look like my pic.jpg (one spaces) or my pic.jpg (three spaces) would come out as my_pic.jpg (one underscores) or my___pic.jpg(three underscores).

$filename = 'my pic.jpg';   //your file name...
$filename = preg_replace('/\s+/', '_', $filename);

output : my_pic.jpg  //you get this output...

Upvotes: 1

Gabe
Gabe

Reputation: 971

Simply use str_replace to replace all white spaces with another string:

$fileName = str_replace(" ", "_", $fileName);

Upvotes: 1

Related Questions