php - 上传后文件名显示不正确 - PHP

标签 php windows file-upload encoding

我正在使用 xampp 运行 Win7 64。 我正在上传一个文件名包含希腊字符的文件。 文件名存储不正确 例如,ελλnνικά.xlsx 存储为 ΞμλληνΞΞΞΊΞς.xlsx。

我想它必须与编码有关。

在我的 html 中我使用

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

下面给出了我的 JavaScript

  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);}    
  });
}    

服务器代码如下

    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.';
    }

我在 php 文件中放置了一个断点并进行了检查
$_FILES[文件][名称]。
文件名似乎是正确的。文件内容上传也没有错误。

我猜当文件从临时目录移动到上传目录时发生了一些事情 但我没有主意了:(

最佳答案

我对西类牙语字符也有同样的问题。就我而言,我使用了 utf8_decode()功能并且成功了!

尝试

$file_name = utf8_decode($file_name);

关于php - 上传后文件名显示不正确 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15315861/

相关文章:

windows - 如何使用 UTF-8 编码编织 2html?

windows - Git LFS 不考虑 credential.helper 的配置文件级别

perl - Perl中上传文件的现代核心方法

Jquery BlueImp 如何在上传前更改文件名?

php - MySQL 选择日期范围内的所有日期,即使当天没有可用数据

javascript - 如何将选择/下拉值传递给表单2?

php - MySQL Left Join 不等于

php - 如何将多个 PHP 变量传递给 jQuery 函数?

windows - midl 找不到 C 预处理器 cl.exe

ajax - javax.servlet.ServletException 请求不包含多部分/表单数据或多部分/混合流