Jump to content

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


bradkenyon

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']));

Archived

This topic is now archived and is closed to further replies.

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