Jump to content

[SOLVED] file upload, need to lower case filename, as well as replace space w/ underscore


Recommended Posts

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.";
	}
}?>

 

 

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

riddled with syntax errors:

 

$illegal_characters = Array(" ", "%20", ";", "#", "~");
$newname = strtolower(str_replace($illegal_characters,"_", $_FILES['doc_uploaded']['name']));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.