我的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>
答案 0 :(得分:0)
首先我添加了setlocale(),因为Laci K在download.php上说,当我下载非英文命名文件时,名字不是空白。但是它是%AC%BC%58%..更多%something.php。然后我改变了urlencode(basename($ file)));只是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;
}
?>