链接适用于所有浏览器,除了 safari(php 代码)


Link works in all browsers except safari (php code)

>我创建了一个网站,显示特定文件夹(命名文档)的文件和目录。用户可以浏览目录,如果他选择一个文件(pdf文档等),它会下载它。问题是它适用于所有浏览器,但特别是在 safari 中,如果用户选择一个文件(pdf 文档等),它不会下载它,并且会弹出一个新窗口,例如:

请求的网址/root/documents/2013-01-13 1ηΈκÏ"ακÏ"η- 1·Î•Î£/Î ÏακÏ"ικά 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="&lt;Directory&gt;"; 
                            $size=folderSize($MYCDIR."/".$dirArray[$index]); 
                            $size=number_format($size/1024,2). ' KB';
                            $class="dir";
                        } 
                        else {
                            $class="file";
                        }

                        if($name=="."){
                            $name=". (Current Directory)"; 
                            $extn="&lt;System Dir&gt;";
                        }
                        if($name==".."){
                            $name=".. (Parent Directory)"; 
                            $extn="&lt;System Dir&gt;";
                        }
                          //echo $namehref;
                          //<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Here is the problem
                        if ($extn!="&lt;Directory&gt;"){
                            $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>
if ($encoding !== 'UTF-8'){
        return mb_convert_encoding( $source, 'UTF-8','UTF-8');
    }
您正在从 UTF-8 编码

转换为 UTF-8 编码。结果,野生动物园似乎在不受支持的编码中获取网址,这似乎无法正确处理。

尝试:

if ($encoding !== 'UTF-8'){
        return mb_convert_encoding( $source, 'UTF-8','UTF-8,ISO-8859-7 ');
    }

更新

我做了一些额外的测试,如果将"auto"指定为属性或没有传递第二个属性,似乎mb_detect_encoding()总是返回 UTF-8。我通过如下所示的方式解决了这个问题,但是如果没有实际的文件和路径,则无法完全测试它。试试它是否以这种方式工作:

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;
    }
}