Replace spaces with subtrees in the downloaded file

I have a basic script that allows my site members to download short samples of their compositions so that clients can listen to them.

Some file names have spaces, which obviously cause problems when trying to play samples. Is there a way to replace these spaces with underscores between loading into the temp directory and moving the file to the 'samples' directory?

<? $url = "http://domain.com/uploads/samples/"; //define the upload path $uploadpath = "/home/public_html/uploads/samples"; //check if a file is uploaded if($_FILES['userfile']['name']) { $filename = trim(addslashes($_FILES['userfile']['name'])); //move the file to final destination if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadpath."/". $filename)) { echo " bla bla bla, the final html output "; } else{ if ($_FILES['userfile']['error'] > 0) { switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4: echo 'No file uploaded'; break; } exit; } } } ?> 
+4
source share
3 answers

After this line:

 $filename = trim(addslashes($_FILES['userfile']['name'])); 

Record:

 $filename = str_replace(' ', '_', $filename); 

A file name, for example hello world.mp3 (two spaces), will look like hello__world.mp3 (two underscores) to avoid this:

 $filename = preg_replace('/\s+/', '_', $filename); 
+10
source
 $filename = str_replace(' ', '-', trim(addslashes($_FILES['userfile']['name']))); 

Why addslashes though? It also seems too simple - am I missing something?

0
source

to try

 $filename = trim(str_replace(" ","_", $_FILES['userfile']['name'])); 
0
source

Source: https://habr.com/ru/post/1412635/


All Articles