1

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

6
  • 1
    The code you posted does not seem to be completely relevant to what you demand Commented Dec 19, 2014 at 10:54
  • 1
    You need to show the code of real upload (with $_FILES) that said you can do a $filename = str_replace(" ","_",$_FILES['myFile']['tmp_name']); and use $filename Commented Dec 19, 2014 at 10:55
  • @Gabe Whoops; changed it Commented Dec 19, 2014 at 10:57
  • There is no code now. Commented Dec 19, 2014 at 10:59
  • @MarcoMura Okay, can I just have the PHP to do it then, not my code edited? :D Commented Dec 19, 2014 at 10:59

2 Answers 2

1

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

$fileName = str_replace(" ", "_", $fileName);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.