2

I'm working on Win7 64 with xampp. I'm uploading a file which has a filename with greek characters. Filename is stored incorrectly e.g ελληνικά.xlsx is stored as ελληνικά.xlsx.

I guess it has to do something with encoding.

In my html I'm using

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

My javascript is given below

  function handleFileSelect(evt) { 
  var output=[];

  if (evt.target.files.length === 0 ) exit();
  var f = evt.target.files[0];
  if ($.inArray(f.name.split('.').pop(), ['xls', 'xlsx']) === -1 ) {
    $('#output').html('Wrong file type. Only Excel files are valid.');
    exit;
  }

  ans = fileunit(f.size);    
  output.push('<li><strong>', f.name, ' - ',
              ans[0].toFixed(2), ans[1], '</li>');
  $('#list').html('<ul>' + output.join('') + '</ul>');

  var fd = new FormData();
  fd.append('file', evt.target.files[0]); 

  $.ajax({
    url: 'uploaddata/file',
    data:fd,
    processData: false,
    contentType: false,
    type: 'POST',    
    success: function(data){
      $('#output').html(data);},
    error: function(data)  {
      $('#output').html(data);}    
  });
}    

Server code is given below

    public function upload() {
// Checking upload error code
  if ($_FILES["file"]["error"] > 0)
    {
       echo file_upload_error_message($_FILES["file"]["error"]);
       return;
    }

  //Checking uplaod directory  
  $uploadDirectory = 'uploads' . DIRECTORY_SEPARATOR;
  if(!is_dir($uploadDirectory))
    {
      @mkdir ($uploadDirectory, 0766, true);
    }
  if(!is_dir($uploadDirectory))
    {
      echo 'Server error. Impossible to create the upload folder.';
      return;
    }
  elseif(!is_writable($uploadDirectory))
    {
      echo 'Server error. Upload directory is not writable.';
      return;
    }  

  //Checking if file is too big  
  if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
         empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0) 
    {
       $error = 'Server error. File is too large, cannot upload.';
       echo $error;
       return;
    }

  $file_name = $_FILES["file"]["name"];
  if (!move_uploaded_file($_FILES["file"]["tmp_name"], $uploadDirectory.$file_name))
    {
      echo 'Server error. Error moving uploaded file from temp dir to upload dir';
      return;
    }
   echo $file_name . ' was uploaded successfully.';
    }

I placed a breakpoint in php file and I checked
$_FILES[file][name].
Filename appears to be correct. Also file content uploads with no errors.

I guess that something is happening when file is moved from temp dir to upload dir but I'm out of ideas :(

2
  • 2
    You should not only validate the user’s input on the client side but especially on the server side. Currently, a user can upload any file, including PHP files. Commented Mar 9, 2013 at 21:08
  • @Gumbo Thanks for your comment, it is a work in progress. All user input will be server side validated. Commented Mar 10, 2013 at 4:50

3 Answers 3

2

I had the same problem with Spanish characters. In my case, i used the utf8_decode() function and it worked!

Try

$file_name = utf8_decode($file_name);
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you need to urlencode() the name on the server side, and then urldecode() for display. Your server is just not handling Unicode properly.

Comments

0

As you say that the filename appears correctly (in Greek letters), you just need to convert them to utf-8. You could simply use mb_convert_encoding.

$file_name = mb_convert_encoding($_FILES[file][name], 'utf-8');

1 Comment

I have used mb_convert_encoding with no luck :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.