2014-09-27 61 views
1

我的php项目是关于在列表中显示文件和目录,用户可以在文件目录之间浏览,通过文件名称和其他名称搜索文件。我的问题是,如果我搜索一个文件,它会发现它(不管它的名字是英语还是希腊语),但如果我选择下载的文件不是英文,它的名称将为空。例如,如果它的名字是“σουβλάκι.php”,并且我选择下载它,那么我将下载的填充名为“.php”。我相信它与utf-8有关,但我无法弄清楚。任何想法都将非常有用。 在情况下,它可以帮助,这是我的download.php代码:php项目,非英文名称的文件在下载时有空白名称

<?php 
setlocale (LC_ALL, "el_GR.UTF-8"); 
$file = $_GET["file"]; 
if (mb_detect_encoding($file)=="ISO-8859-1"){ 
$file = utf8_encode("$file"); 
} 
if (file_exists($file)) { 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header("Content-Type: application/force-download"); 
header('Content-Disposition: attachment; filename=' . urlencode(basename($file))); 
// header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 
} 
?> 

,这是我的index.php

<?php 

    define("SUBFOLDER","http://192.168.1.3/webserver"); 
    define("ROOT","/var/www/webserver"); 
?> 

<head> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<link href="<?php echo SUBFOLDER."/"; ?>css/myCSSfile.css" rel="stylesheet" type="text/css"> 
<link rel="shortcut icon" href="<?php echo SUBFOLDER."/"; ?>images/dit.ico"> 
<link rel="stylesheet" href="<?php echo SUBFOLDER."/"; ?>css/search.css"> 
<link rel="stylesheet" href="<?php echo SUBFOLDER."/"; ?>css/button.css"> 
<link rel="stylesheet" href="<?php echo SUBFOLDER."/"; ?>css/button2.css"> 
<script type="text/javascript" src="<?php echo SUBFOLDER."/"; ?>js/resolutionfinder.js"></script> 
<script type="text/javascript" src="<?php echo SUBFOLDER."/"; ?>js/changeInputValue.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript" src="<?php echo SUBFOLDER."/"; ?>js/ajaxcalls.js"></script> 





<body onload='ShowDivInCenter();' onresize='ShowDivInCenter();'> 
    <div class="cont"> 
     <div id="main"> 
      <?php 
       error_reporting(E_ALL); 
       if ($_GET['action']=="view"){ 
        include_once("foldercontents.php"); 
       } 
       else if ($_GET['action']=="downloadZip"){ 
        include_once("downloadZip.php"); 
       } 
       else if ($_GET['action']=="downloadfile"){ 
        include_once("download.php"); 
       } 
       else { 
        include_once("foldercontents.php"); 
       } 
      ?> 
     </div> 
    </div> 
</body> 
+1

你尝试'函数utf8_encode()'文件名? – 2014-09-27 00:28:15

+0

我在第3行的download.php中添加了这个。我还在某处读到我不应该在index.php上使用所以我也删除了它。虽然结果是一样的。 – 2014-09-27 17:21:31

+1

哦,我刚刚看到你正在使用'basename()',如果你需要使用'basename()',你必须在你的情况下使用'basename()''''''''''''''''''''''''''''''''''''''''' (LC_ALL,'希腊');'也许''setlocale(LC_ALL,'gr_GR.UTF8');' – 2014-09-27 18:03:29

回答

0

首先我加入的setlocale()作为拉奇钾对的download.php当说我下载非英文命名文件的名称不是blank.php,但它是%AC%BC%58%.. more%something.php。然后我改变了urlencode(basename($ file)));到基地名称($文件);现在它工作正常。如果因为不需要,我也删除了第一个。 这是前:

<?php 
$file = $_GET["file"]; 
if (mb_detect_encoding($file)=="ISO-8859-1"){ 
$file = utf8_encode("$file"); 
} 
if (file_exists($file)) { 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header("Content-Type: application/force-download"); 
header('Content-Disposition: attachment; filename=' . urlencode(basename($file))); 
// header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 
} 
?> 

,这是正确的:

<?php 
setlocale (LC_ALL, "el_GR.UTF-8"); 
$file = $_GET["file"]; 
if (file_exists($file)) { 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header("Content-Type: application/force-download"); 
header('Content-Disposition: attachment; filename=' . basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 
} 
?>