Removing spaces in file names of uploaded files using php

Hi all i am trying to make my upload system remove spaces of uploaded files in a file name of a uploaded file this is my code i have atm

?>
						<table>
						<form enctype="multipart/form-data" action="" method="post">
							<tr>
								<th>Song Name:</th>
								<th><input type="text" name="songname"></th>
							</tr>
							<tr>
								<th>Artist</th>
								<th><input type="text" name="artist"></th>
							</tr>
							<tr>
								<th>Music File</th>
								<th><input type="file" name="userfile"></th>
							</tr>
							<tr>
								<th><input type="submit" name="upload" value="Upload"></th>
							</tr>
						</form>
						</table><?php
						//
						if(isset($_POST['upload']))
						{
							$userfile=$_FILES['userfile'];
							$songname=$_POST['songname'];
							$artist=$_POST['artist'];
							$songname2=$_FILES['userfile']['name'];
							if($userfile || $songname || $artist)
							{
								//
								$path = "mp3/";
								$path2="mp3/".$_POST['userfile']['name']."";
									$max_size = 20000000;
									if(!isset($HTTP_POST_FILES['userfile']))
									{
										exit;
									}						
									if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
									{
										if ($HTTP_POST_FILES['userfile']['size']>$max_size)
										{
											echo "The file is too big<br>\
";
											exit;
										}
										if (($HTTP_POST_FILES['userfile']['type']=="audio/mpeg"))
										{
											if (file_exists($path . $HTTP_POST_FILES['userfile']['name']))
											{
												echo "The file already exists<br>\
";
												exit;
											}
											$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
											$HTTP_POST_FILES['userfile']['name']);
											if (!$res)
											{
												echo "upload failed!<br>\
";
												exit;
											}
											else
											{
												include("dbconnect.php");
												$rQuery="INSERT into music values('','$songname','$path$songname2','$artist')";
												$rs=mysqli_query($con,$rQuery);
												if(!$rs)
												{
													echo "Error".mysqli_error($con);
												}
												else
												{
													echo "upload sucessful<br>\

												The File has been placed in the MP3 folder<br/>";
												echo "File is&nbsp;".$path2."".$songname2."";
												}
												
											}
										}
										else
										{
											echo "Wrong file type<br>\
";
											exit;
										}
									}
								
								//
							}
						}

That is what i have so far is there any other way i can do that removing spaces of file names of files uploaded?

when you copy the file, define the filename before it


$filename = str_replace(" ", "_", $_FILES['userfile']['name']);
$res = copy($_FILES['userfile']['tmp_name'], $path . $filename);

then use the $filename variable in your database insert.

Also change $HTTP_POST_FILES to $_FILES unless you are using a very old version on PHP

I wouldn’t use the original file name while storing the database. At least I would append any id or random string in the file because user might upload another file with the same name then the previous file will be replaced. So try to rename the original file.

Though you have checking whether the same named file is already existed or not but the user may not know what filenames are already used.