【发布时间】:2014-09-27 00:00:21
【问题描述】:
我的 php 项目是关于在列表中显示文件和目录,用户可以在文件目录中浏览,按文件名搜索文件等。我的问题是,如果我搜索一个文件,它会找到它(不管它的名字是英文还是希腊文),但如果我选择下载的文件不是英文的,它的名字将是空白的。例如,如果它的名称是“σουβλάκι.php”并且我选择下载它,我将下载的填充将被命名为“.php”。我相信它与 utf-8 有关,但我无法弄清楚。任何想法都会非常少见。 如果有帮助,这是我的 download.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>
【问题讨论】:
-
你试过
utf8_encode()文件名吗? -
我在第 3 行的 download.php 中添加了这个。我还在某处读到我不应该在 index.php 上使用 所以我也删除了它.结果是一样的。
-
哦,我刚刚看到您正在使用
basename(),如果您需要使用它,那么您必须在使用basename()之前设置语言环境,在您的情况下,我会在文件开头设置本地(),例如这个setlocale(LC_ALL, 'greek');或者setlocale(LC_ALL, 'gr_GR.UTF8'); -
首先感谢您的时间!我尝试使用 setlocale 也看到了一些关于希腊语的例子,但没有帮助。如果您有任何其他想法,我们非常欢迎。
-
Ι 使用了错误的语言环境。我使用了正确的,并且知道它没有显示 .php 作为它的名称,它显示 %CE%BF%85 和更多 %something.php。
标签: php html apache utf-8 character-encoding