Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace special characters before the file is uploaded using PHP

I was wondering if it is possible to change the name of the file to be uploaded. I mean what I am trying to do is that, the user uploads a file which may have some special characters like special characters in some European languages.

What I am planning to do is that before using the move_uploaded_file command is it possible to change/preg_replace the special characters with normal characters, so that the file is uploaded and stored with the new name which has only normal characters.

like image 872
125369 Avatar asked Mar 23 '12 12:03

125369


2 Answers

// Get the original file name from $_FILES
$file_name= $_FILES['file']['name'];

// Remove any characters you don't want
// The below code will remove anything that is not a-z, 0-9 or a dot.
$file_name = preg_replace("/[^a-zA-Z0-9.]/", "", $file_name);

// Get the location of the folder to upload into
$location = 'path/to/dir/';

// Use move_uploaded_file()
move_uploaded_file($_FILES["file"]["tmp_name"], $location.$file_name);
like image 176
472084 Avatar answered Oct 21 '22 07:10

472084


try to use this bro

   $result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

to know more visit how to replace special characters with the ones they're based on in PHP?

like image 43
joni_demon Avatar answered Oct 21 '22 07:10

joni_demon