20

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.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
125369
  • 3,547
  • 20
  • 52
  • 70

5 Answers5

31
// 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);
472084
  • 17,666
  • 10
  • 63
  • 81
  • @472084, You have solved my so long problem with special characters in Zip file and under sub directories or files. Great. :) – Smile Sep 04 '13 at 07:59
  • 1
    this works great, but you have to add the dot too for the file extension. $file_name = preg_replace("/[^a-zA-Z0-9.]/", "", $file_name); – Alex Angelico Aug 20 '14 at 15:43
  • it works but didn't change/rename the file name on path location – jned29 Oct 22 '14 at 08:01
  • Note that depending on your use case, you _may_ wish to remove the dots as well, just in case someone tries `../../../passwd`. It probably doesn't matter though. – starbeamrainbowlabs Oct 19 '15 at 07:00
3

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?

Community
  • 1
  • 1
joni_demon
  • 656
  • 6
  • 12
0

Also you can use a function for special characters like this:

function safename($theValue)
{
    $_trSpec = array(
        'Ç' => 'C', 
        'Ğ' => 'G', 
        'İ' => 'I',
        'Ö' => 'O', 
        'Ş' => 'S', 
        'Ü' => 'U',
        'ç' => 'c', 
        'ğ' => 'g', 
        'ı' => 'i',
        'i' => 'i',
        'ö' => 'o', 
        'ş' => 's', 
        'ü' => 'u',
    );
    $enChars = array_values($_trSpec);
    $trChars = array_keys($_trSpec);
    $theValue = str_replace($trChars, $enChars, $theValue); 
    $theValue=preg_replace("@[^A-Za-z0-9\-_.\/]+@i","-",$theValue);
    $theValue=strtolower($theValue);
    return $theValue;
}

Be carefull about allow . for file extension.

And then change your original temp file name,

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetFile = safename($targetFile);

$location = 'path/to/dir/';
move_uploaded_file($_FILES["file"]["tmp_name"], $location.$targetFile);
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
canerkoroglu
  • 67
  • 1
  • 8
0

You can get the original filename for an uploaded file from $_FILES, and you can create your "special" version by replacing characters in it with strtr (which sounds as the best match for this case), str_replace, preg_replace or any other string processing function.

The best approach depends on what exactly you want to do.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

You could do it like this, write a simple function strip_special_chars() to replace characters you want in a string

$tmp_name = $_FILES["file"]["tmp_name"];
$name = strip_special_chars($tmp_name);
move_uploaded_file($name, "path/to/dir/");
slash197
  • 9,028
  • 6
  • 41
  • 70