PHP Upload - Spaces in File Name

给你一囗甜甜゛ 提交于 2021-02-19 23:49:06

问题


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. :)


回答1:


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

$fileName = str_replace(" ", "_", $fileName);
  • Doc: php.net



回答2:


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...


来源:https://stackoverflow.com/questions/27564501/php-upload-spaces-in-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!