dqusbxh44823 2015-03-13 19:37
浏览 73

链接适用于除Safari之外的所有浏览器(PHP代码)

I've created a website which shows the files and directories of a specific folder(named documents). The user can navigate through directories and if he choose a file (pdf doc etc) it downloads it. The problem is that it works in all browsers but specifically in safari if the user chooses a file (pdf doc etc) it won't download it and a new window will pop saying for example:

The requested URL /root/documents/2013-01-13 1η Έκτακτη - 1η ΕΣ/Î Ïακτικά 1ης Εκτακτης Γ.Σ. TÎœ.ΠΛΗΡΟΦΟΡΙΚΗΣ ΚΑΙ ΤΗΛΕΠΙΚΟΙÎΩÎΙΩÎ.doc was not found on this server.

The weird letters suppose to be greek. I'm trying to figure out why and the fact that it hapens only in safari seems really weird to me. Here is the code that is responsible for the content of the links. I have put a big arrow as a comment above the code that's responsible for the links of files (it's inside "if"). Any idea would be really helpful.

<?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>
  • 写回答

1条回答 默认 最新

  • duanpai9945 2015-03-13 20:06
    关注
    if ($encoding !== 'UTF-8'){
            return mb_convert_encoding( $source, 'UTF-8','UTF-8');
        }
    

    You are converting from UTF-8 encoding to UTF-8 encoding. As result safari seems to get url in unsupported encoding which seems not to handle properly.

    Try:

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

    UPDATE

    I did some additional testing and it seems that mb_detect_encoding() always returns UTF-8 if "auto" is specified as attribute or no second attribute is passed. I solved this by doing that like below, but can't fully test it without the actual files and paths. Try if it works this way:

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

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计