0

I'm trying to make a website where you can upload flash .swf files. It works great and all except whenever a user uploads a flash file name with japanese or any other asian characters (tested with Japanese characters) it gives me the name wrong.

So this is the file: enter image description here

Before it starts moving the file im putting the information in the database.
Database:

flash_id | flash_name | not relevant | flash_size | flash_date

enter image description here

After putting the flashes information in the database I move the file from the tmp folder to another folder where I store the uploads. enter image description here

If you compare the file names from the first 2 pictures with the third one you can see it isn't the same.

Here is the code:

The variables map, filename and ext below are the values that were used in the moving process.

$map = 62;
$filename = $_FILES['upfile']['name'];
$ext = swf;

$str = "../uploads/$map/$filename.$ext";

if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        //sprintf('../uploads/%s/%s.%s',$map,$filename,$ext)
        $str
    )) {
        // Failed to move uploaded file.
        throw new RuntimeException(4);
    } else {
        try {
            $insq = $pdo->prepare('INSERT INTO flashes (flash_name, flash_size) VALUES (?,?)');
            $insq->bindParam(1, $filename);
            $insq->bindParam(2, $filesize);
            $insq->execute();
        } catch (PDOException $ex) {
            throw new RuntimeException(999);
        }
    }
    // File is uploaded successfully.
    echo 10;
Efekan
  • 1,497
  • 12
  • 31
  • sprintf isn't multi-byte string capable. and for such an absurdly simple string building exercise, you'd be better off just having `$str = "../uploads/$map/$filename.$ext"` anyways. printf/sprintf are really only useful for formatting numbers, e.g. `%3.7f` type things. – Marc B Aug 25 '14 at 18:39
  • @MarcB I see. I've changed the sprintf with just a normal string. However this has not help fix the problem. – Efekan Aug 25 '14 at 18:49
  • Plus, you're dealing with a database. are you sure that the db connection and db tables are set to the proper character set as well? – Marc B Aug 25 '14 at 18:54
  • @MarcB Yes they are all in UTF-8. However I am only putting the flash info in the database. The variables $filename and $ext I get are not from the database but from the flash itself. So the $filename and $ext are the same ones used to put the information in the database. – Efekan Aug 25 '14 at 18:59
  • are you sure that your conection is utf8? see [this answer](http://stackoverflow.com/a/4361485/453348) – tttony Aug 25 '14 at 19:20
  • @tttony Yes I am sure. Also the filename is not put from the database. But from the uploaded flash itself. – Efekan Aug 25 '14 at 19:42
  • so the filename is correctly saved in the database, the problem is when you show the filename? – tttony Aug 25 '14 at 19:58
  • @tttony The problem is that when I move the file to another folder the files name is not correct anymore just like in the post pictures. The database shows that the name is correct up until the moment its used to rename the tmp_name with name. – Efekan Aug 25 '14 at 20:10
  • ok the problem is that the file name is changed in the filesystem, take a look [this answer](http://stackoverflow.com/a/18481737/453348), but I suggest that save the japanese file name in the db and create an unique name for the file but with ascii characters, and save it that in the db – tttony Aug 25 '14 at 22:07

1 Answers1

0

I could not find a solution for this. So instead of trying to fix this I followed tttony's comment and made it so that the file_names are randomized and the file_name before randomizing is put in the database. So we can show it on our website with its original name however when we are going to download it we will receive a randomized file_name. However if you use the html download="original_file_name" attribute you can still make the user download it with the original file_name.

Efekan
  • 1,497
  • 12
  • 31