2016-10-07 10 views
-3

以下のファイルエクスプロアスクリプトにフォースダウンロード機能を追加します。 broswerでファイルを開くのではなく、$ namehrefをクリックすると、ユーザーにファイルをダウンロードさせる必要があります。 これをコードに追加するにはどうすればよいですか?ファイルエクスプローラコード - 強制ダウンロードオプションなし

 <?php 





     // Adds pretty filesizes 
     function pretty_filesize($file) { 
      $size = filesize($file); 
      if ($size < 1024) { 
       $size = $size . " Bytes"; 
      } elseif (($size < 1048576) && ($size > 1023)) { 
       $size = round($size/1024, 1) . " KB"; 
      } elseif (($size < 1073741824) && ($size > 1048575)) { 
       $size = round($size/1048576, 1) . " MB"; 
      } else { 
       $size = round($size/1073741824, 1) . " GB"; 
      } 
      return $size; 
     } 

     date_default_timezone_set("Europe/Rome"); 

     // Checks to see if veiwing hidden files is enabled 
     /* 
      if ($_SERVER['QUERY_STRING'] == "hidden") { 
      $hide = ""; 
      $ahref = "./"; 
      $atext = "Hide"; 
      } else { 

      $hide = "."; 
      $ahref = "./?hidden"; 
      $atext = "Show"; 
      } 
     */ 

     $hide = "."; 

     if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) { 
      $currdir = "."; 
     } else { 
      $currdir = urldecode($_SERVER['QUERY_STRING']); 
     } 

     if ($currdir == ".") 
      $label = "Root"; 
     else { 
      $path = explode('/', $currdir); 
      $label = $path[count($path)-1]; 
     } 

     ?> 
     <!doctype html> 
     <html> 
     <head> 
      <meta charset="UTF-8"> 
      <link rel="shortcut icon" href="./favicon.ico"> 
      <link rel="stylesheet" href="favicon.ico"> 
      <title>DPP</title> 


      <link rel="stylesheet" type="text/css" href="style.css"> 
      <script src="sorttable.js"></script> 
     </head> 

     <body> 
     <div id="container" style="margin-top:-50px"> 
     <table class="sortable"> 
       <thead> 
       <tr> 
        <th bgcolor="#003399">Filename</th> 
        <th bgcolor="#003399">Type</th> 

       </tr> 
       </thead> 
       <tbody><?php 
     error_reporting(0); 
     // Opens directory 
     $myDirectory = opendir($currdir); 

     // Set Forbidden Files 
     $forbiddenExts = array("php", "ico", "html", "LCK", "js", "css"); 

     // Gets Each Entry 
     while($entryName = readdir($myDirectory)) { 
     $exts = explode(".", $entryName); 
      if(!in_array($exts[1],$forbiddenExts)) { 
      $dirArray[] = $entryName; 
      } 
     } 

     // Closes directory 
     closedir($myDirectory); 

     // Counts elements in array 
     $indexCount = count($dirArray); 

     // Sorts files 
     //sort($dirArray); 

     // Loops through the array of files 
     for ($index = 0; $index < $indexCount; $index++) { 

      // Decides if hidden files should be displayed, based on query above. 
      if (substr("$dirArray[$index]", 0, 1) != $hide || ($currdir != '.' && $dirArray[$index] == "..")) { 

       // Resets Variables 
       $favicon = ""; 
       $class = "file"; 

       // Gets File Names 
       $name = $dirArray[$index]; 
       $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index]; 
      $fullname = $currdir . '/' . $dirArray[$index]; 

       // Gets Date Modified 
       $modtime = date("M j Y g:i A", filemtime($fullname)); 
       $timekey = date("YmdHis", filemtime($fullname)); 


       // Separates directories, and performs operations on those directories 
       if (is_dir($currdir . '/' . $dirArray[$index])) { 
        $extn = "&lt;Folder&gt;"; 
        $size = "&lt;Folder&gt;"; 
        $sizekey = "0"; 
        $class = "dir"; 

        // Gets favicon.ico, and displays it, only if it exists. 
        if (file_exists("$namehref/favicon.ico")) { 
         $favicon = " style='background-image:url($namehref/favicon.ico);'"; 
         $extn = "&lt;Website&gt;"; 
        } 

        // Cleans up . and .. directories 
        if ($name == ".") { 
         $name = ". (Current Directory)"; 
         $extn = "&lt;System Dir&gt;"; 
         $favicon = " style='background-image:url($namehref/.favicon.ico);'"; 
        } 
        if ($name == "..") { 
         $name = ".. (Return to Parent Folder)"; 
         $extn = "&lt;System Dir&gt;"; 
        } 
        if ($currdir == "." && $dirArray[$index] == "..") 
         $namehref = ""; 
        elseif ($dirArray[$index] == "..") { 
         $dirs = explode('/', $currdir); 
         unset($dirs[count($dirs) - 1]); 
         $prevdir = implode('/', $dirs); 
         $namehref = '?' . $prevdir; 
        } 
        else 
         $namehref = '?' . $namehref; 
       } 

       // File-only operations 
       else { 
        // Gets file extension 
        $extn = pathinfo($dirArray[$index], PATHINFO_EXTENSION); 

        // Prettifies file type 
        switch ($extn) { 
         case "png": $extn = "PNG Image"; 
          break; 
         case "jpg": $extn = "JPEG Image"; 
          break; 
         case "ppsx": $extn = "Microsoft Power Point"; 
          break; 
         case "jpeg": $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 "html": $extn = "HTML File"; 
          break; 
         case "xhtml": $extn = "HTML File"; 
          break; 
         case "shtml": $extn = "HTML File"; 
          break; 
         case "ppt": $extn = "Microsoft Power Point"; 
          break; 
         case "js": $extn = "Javascript File"; 
          break; 
         case "css": $extn = "Stylesheet"; 
          break; 

         case "pdf": $extn = "PDF Document"; 
          break; 
         case "xls": $extn = "Spreadsheet"; 
          break; 
         case "xlsx": $extn = "Spreadsheet"; 
          break; 
         case "doc": $extn = "Microsoft Word Document"; 
          break; 
         case "docx": $extn = "Microsoft Word Document"; 
          break; 

         case "zip": $extn = "ZIP Archive"; 
          break; 
         case "htaccess": $extn = "Apache Config File"; 
          break; 
         case "exe": $extn = "Windows Executable"; 
          break; 

         default: if ($extn != "") { 
           $extn = strtoupper($extn) . " File"; 
          } else { 
           $extn = "Sconosciuto"; 
          } break; 
        } 

        // Gets and cleans up file size 
        $size = pretty_filesize($fullname); 
        $sizekey = filesize($fullname); 
       } 

       // Output 
       echo(" 
       <tr class='$class'> 
        <td><a href='$namehref'$favicon class='name'>$name</a></td> 
        <td><a href='$namehref'>$extn</a></td> 

       </tr>"); 
       } 
      } 
     ?> 

       </tbody> 
      </table> 


     </div> 
     </body> 
     </html> 
+5

上方に配置されなければならない[PHPでファイルのダウンロードを強制する方法]の可能な重複(http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) –

+1

'error_reporting(0);'常に危険な記号です。より少ない$ 100のためにこれの近くのどこにも行っていない – RiggsFolly

+0

これはPHPフォースダウンロードのために@ Al Foicceѫ – user3315848

答えて

0

使用の行:

header("Content-Disposition: attachment;Filename={FILE_NAME}"); 

それはヘッダ情報を変更するので、それはすべてのecho S

+0

コード内にこの行を挿入する必要があります@jameson – user3315848

+0

ファイルを取得するPHPスクリプトの冒頭に –

+0

ああ、私は今理解している、あなたは非常に混乱している。あなたはそれを扱う新しいPHPページを作らなければなりません。ここを見てくださいhttp://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click- for-pdf-in-html –

0
header('Content-type: application/ms-excel'); // file type 
    header('Content-Description: File Transfer'); 
    header("Content-Disposition: attachment; filename=\"" .$filename . "\""); 
    header("Cache-control: private"); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: no-cache');