【问题标题】:Filename not appearing correct after upload - PHP上传后文件名显示不正确 - PHP
【发布时间】:2013-03-09 20:59:19
【问题描述】:

我正在使用 xampp 开发 Win7 64。 我正在上传一个文件名带有希腊字符的文件。 文件名存储不正确 例如 ελληνικά.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[文件][名称]。
文件名似乎是正确的。文件内容上传也没有错误。

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

【问题讨论】:

  • 您不仅应该在客户端验证用户的输入,尤其是在服务器端。目前,用户可以上传任何文件,包括 PHP 文件。
  • @Gumbo 感谢您的评论,这是一项正在进行的工作。所有用户输入都将经过服务器端验证。

标签: php windows file-upload encoding


【解决方案1】:

我在西班牙语字符方面遇到了同样的问题。就我而言,我使用了utf8_decode() 函数并且它有效!

试试

$file_name = utf8_decode($file_name);

【讨论】:

    【解决方案2】:

    也许你需要在服务器端urlencode()这个名字,然后urldecode()才能显示。您的服务器只是没有正确处理 Unicode。

    【讨论】:

      【解决方案3】:

      正如您所说的文件名正确显示(希腊字母),您只需将它们转换为utf-8。你可以简单地使用mb_convert_encoding

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

      【讨论】:

      • 我使用 mb_convert_encoding 没有运气:(
      猜你喜欢
      • 2015-07-20
      • 2016-12-21
      • 2012-04-27
      • 2014-10-05
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 2014-10-23
      • 2013-09-22
      相关资源
      最近更新 更多