bradkenyon Posted August 27, 2008 Share Posted August 27, 2008 I have an document upload form. <input type="file" name="doc_uploaded"> The below code is how I grab the file the user browsed on their hard drive and uploaded. I want to the filename to be all lowercase and replace any spaces w/i the filename w/ underscores, which will take some regex. Could anyone help, thanks! <?php $fileget_doc=$_FILES['doc_uploaded']['name']; if($fileget_doc) { $filename_doc = date(mdyhms).$fileget_doc; } else { $filename_doc = ''; } if($filename_doc != '') { $target_doc = "/web/images/news/"; $target_doc = $target_doc . basename( date(mdyhms).$_FILES['doc_uploaded']['name']); $ok=1; if(move_uploaded_file($_FILES['doc_uploaded']['tmp_name'], $target_doc)) { //echo "The file ".basename( $_FILES['doc_uploaded']['name'])." has been doc_uploaded"; } else { //echo "Sorry, there was a problem uploading your file."; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/121599-solved-file-upload-need-to-lower-case-filename-as-well-as-replace-space-w-underscore/ Share on other sites More sharing options...
Psycho Posted August 27, 2008 Share Posted August 27, 2008 No regular expression necessary for that: $newname = strtolower(str_replace(' ', '_', $file_name)); Quote Link to comment https://forums.phpfreaks.com/topic/121599-solved-file-upload-need-to-lower-case-filename-as-well-as-replace-space-w-underscore/#findComment-627214 Share on other sites More sharing options...
rtil Posted August 27, 2008 Share Posted August 27, 2008 and to be extra safe, you could also write an array of symbols you'd want to be replaced by an underscore, like this: illegal_characters = Array(" ", "%20", ";", "#", "~"); $newname = strtolower(str_replace(illegal_characters,"_",$_FILES['doc_uploaded']['name'])); some file uploaders tend to misbehave if there are strange characters present. at least mine did, haha Quote Link to comment https://forums.phpfreaks.com/topic/121599-solved-file-upload-need-to-lower-case-filename-as-well-as-replace-space-w-underscore/#findComment-627217 Share on other sites More sharing options...
akitchin Posted August 27, 2008 Share Posted August 27, 2008 riddled with syntax errors: $illegal_characters = Array(" ", "%20", ";", "#", "~"); $newname = strtolower(str_replace($illegal_characters,"_", $_FILES['doc_uploaded']['name'])); Quote Link to comment https://forums.phpfreaks.com/topic/121599-solved-file-upload-need-to-lower-case-filename-as-well-as-replace-space-w-underscore/#findComment-627221 Share on other sites More sharing options...
bradkenyon Posted August 27, 2008 Author Share Posted August 27, 2008 thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/121599-solved-file-upload-need-to-lower-case-filename-as-well-as-replace-space-w-underscore/#findComment-627234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.