我创建了一个网站,显示特定文件夹(命名文档)的文件和目录。用户可以浏览目录,如果他选择文件(pdf doc等),则下载它。问题是它适用于所有浏览器,但特别是在safari中,如果用户选择一个文件(pdf doc等),它将不会下载它,并会弹出一个新窗口,例如:
请求的URL / root / documents /2013-01-131ηÎÎÏÏ“αÎÎϔη - 1Δ·Î£Î/ÎÏα±ÎºÏ“ιÎÎÎÎÎÎÎ,ΕÎÏϔΠ±ÎÏϓηÏ,Δ。?£。 TÎ.ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΔÎÎÎÎÎÎÎ.doc在此没有找到服务器。
奇怪的字母假设是希腊语。我试图弄清楚为什么以及只在野生动物园中讨厌的事实对我来说似乎很奇怪。以下是负责链接内容的代码。我在代码上面放了一个大箭头作为注释,负责文件的链接(它在“if”里面)。任何想法都会非常有用。
<?php
function convert_to( $source ){
$encoding = mb_detect_encoding( $source, "auto" );
if ($encoding !== 'UTF-8'){
return mb_convert_encoding( $source, 'UTF-8','UTF-8');
}
else {
return $source;
}
}
?>
<div id="container">
<img class="logo" src="images/logo.jpg" title="uop search engine" width="800" height="80" />
<table class="tbl">
<tr>
<td>
<p class="pl" >Πλοήγηση στα πρακτικά των συνελεύσεων</p>
</td>
<td style="text-align:right;">
<form action='' method="post">
<input type='search' id="search" name='s' autocomplete="off">
<input type='submit' id="button" value='Search' class="button1" >
</form>
</td>
</tr>
</table>
<div class="tfclear"></div>
</div>
<div id="container">
<?php
include "showresults.php";
?>
</div>
<table class="sortable">
<thead>
<tr>
<th>Filename</th>
<th>Type</th>
<th>Size <small>(KB)</small></th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<?php
if (!isset($_GET['currentdirectory'])){
$rootDirectory=opendir("./documents");
//$currentDirectory=ROOT;
}
else {
$currentDirectory = $_GET['currentdirectory'];
$rootDirectory=opendir($currentDirectory);
}
//echo $currentDirectory;
//echo $rootDirectory;
starter($rootDirectory);
Function starter($myDirectory){
while($entryName=readdir($myDirectory)) {
$dirArray[]=$entryName;
}
function findexts ($filename) {
$filename=strtolower($filename);
$exts=split("[/\\.]", $filename);
$n=count($exts)-1;
$exts=$exts[$n];
return $exts;
}
closedir($myDirectory);
$indexCount=count($dirArray);
sort($dirArray);
$c=0;
for($index=0; $index < $indexCount; $index++) {
//echo $dirArray[$index];
if($_SERVER['QUERY_STRING']=="hidden"){
$hide="";
$ahref="./";
$atext="Hide";
}
else {
$hide=".";
$ahref="./?hidden";
$atext="Show";
}
if(substr("$dirArray[$index]", 0, 1) != $hide) {
$name=$dirArray[$index];
$namehref=$dirArray[$index];
$extn=findexts($dirArray[$index]);
if (!isset($_GET['currentdirectory'])){
$MYCDIR = "./documents";
}
else {
$MYCDIR = $_GET['currentdirectory'];
//echo $_GET['currentdirectory'];
$MYCDIR = iconv("UTF-8", "UTF-8",$MYCDIR);
if($c==0){
$parent="Γονικός κατάλογος";
print("
<tr class='$class'>
<td><a href='".dirname($_SERVER[PHP_SELF])."'>".$parent."</a></td>
</tr>");
$c=1;
}
}
// echo mb_detect_encoding( $MYCDIR, "auto" );
$size=number_format(filesize($MYCDIR."/".$dirArray[$index])/1024,2). ' KB';
$modtime=date("M j Y g:i A", filemtime($MYCDIR."/".$dirArray[$index]));
$timekey=date("YmdHis", filemtime($MYCDIR."/".$dirArray[$index]));
switch ($extn){
case "png": $extn="PNG Image"; break;
case "jpg": $extn="JPEG Image"; break;
case "svg": $extn="SVG Image"; break;
case "gif": $extn="GIF Image"; break;
case "ico": $extn="Windows Icon"; break;
case "txt": $extn="Text File"; break;
case "log": $extn="Log File"; break;
case "htm": $extn="HTML File"; break;
case "php": $extn="PHP Script"; break;
case "js": $extn="Javascript"; break;
case "css": $extn="Stylesheet"; break;
case "pdf": $extn="PDF Document"; break;
case "zip": $extn="ZIP Archive"; break;
case "bak": $extn="Backup File"; break;
default: $extn=strtoupper($extn)." File"; break;
}
if(is_dir($MYCDIR."/".$dirArray[$index])) {
$extn="<Directory>";
$size=folderSize($MYCDIR."/".$dirArray[$index]);
$size=number_format($size/1024,2). ' KB';
$class="dir";
}
else {
$class="file";
}
if($name=="."){
$name=". (Current Directory)";
$extn="<System Dir>";
}
if($name==".."){
$name=".. (Parent Directory)";
$extn="<System Dir>";
}
//echo $namehref;
//<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Here is the problem
if ($extn!="<Directory>"){
$hrefparameter = convert_to($MYCDIR."/".$namehref);
$hrefparameter = str_replace("./","/",$hrefparameter);
//echo mb_detect_encoding( $hrefparameter, "auto" );
print("
<tr class='$class'>
<td><a target='blank' href='".SUBFOLDER.$hrefparameter."'>".convert_to($name)."</a></td>
<td>$extn</td>
<td>$size</td>
<td sorttable_customkey='$timekey'>$modtime</td>
</tr>");
}
else {
$hrefparameter0 = $MYCDIR."/$namehref";
$hrefparameter = convert_to($MYCDIR."/$namehref");
print("
<tr class='$class'>
<td><a href='".SUBFOLDER."?currentdirectory=".urlencode($hrefparameter)."&cdir=".urlencode($hrefparameter0)."'>".convert_to($name)."</a></td>
<td>$extn</td>
<td>$size</td>
<td sorttable_customkey='$timekey'>$modtime</td>
</tr>");
}
//echo mb_detect_encoding( $hrefparameter, "auto" );
}
}
}
FUNCTION folderSize($dir){
$count_size = 0;
$count = 0;
$dir_array = scandir($dir);
foreach($dir_array as $key=>$filename){
if($filename!=".." && $filename!="."){
if(is_dir($dir."/".$filename)){
$new_foldersize = foldersize($dir."/".$filename);
$count_size = $count_size+ $new_foldersize;
}
else if(is_file($dir."/".$filename)){
$count_size = $count_size + filesize($dir."/".$filename);
$count++;
}
}
}
return $count_size;
}
?>
<div id="showing"></div>
<div id="results" class="results">
</div>
</tbody>
</table>
<form>
<input
type='button'
id="button"
value='DownloadAll'
class="button2"
<?php if (!isset($_GET['currentdirectory'])){ $_GET['currentdirectory']=""; } ?>
onClick="window.location='downloadZip.php?currentdirectory=<?php echo $_GET['currentdirectory']; ?>';" >
</form>
答案 0 :(得分:0)
if ($encoding !== 'UTF-8'){
return mb_convert_encoding( $source, 'UTF-8','UTF-8');
}
您正在从UTF-8编码转换为UTF-8编码。结果safari似乎得到了不受支持的编码的url,似乎没有正确处理。
尝试:
if ($encoding !== 'UTF-8'){
return mb_convert_encoding( $source, 'UTF-8','UTF-8,ISO-8859-7 ');
}
更新
我做了一些额外的测试,似乎mb_detect_encoding()
总是返回UTF-8,如果&#34; auto&#34;被指定为属性或没有传递第二个属性。我通过下面的方式解决了这个问题,但是在没有实际文件和路径的情况下无法完全测试它。尝试以这种方式工作:
function convert_to( $source ){
$encoding = mb_detect_encoding( $source, "Windows-1253,ISO-8859-7,UTF-8" );
if ($encoding !== 'UTF-8'){
return mb_convert_encoding( $source, 'utf-8','Windows-1253,ISO-8859-7,UTF-8');
}
else {
return $source;
}
}