安全下载文件无法在PHP中正确打开

时间:2013-11-21 06:21:50

标签: php wordpress download

嗨朋友我正在使用安全的php文件下载脚本,文件下载成功,但是发布的文件没有打开,当弹出打开你打开文件或保存时,每个文件长度为“20 kb”< / p>

我告诉你我的代码

的index.php

<table class="widefat posts" cellspacing="0" style="margin-top:20px;">

    <thead>

    <tr>

        <th scope="col" width="30%"><a href="javascript:;">Responder</a></th>

        <th scope="col" width="30%"><a href="javascript:;">From</a></th>

        <th scope="col" width="20%"><a href="javascript:;">Subject</a></th>

        <th scope="col" width="35%"><a href="javascript:;">Attachment</a></th>

        <th scope="col" width="10%"><a href="javascript:;">Edit</a></th>    

        <th scope="col" width="10%"><a href="javascript:;">Delete</a></th>  

    </tr>

    </thead>

    <tfoot>

    <tr>

        <th scope="col" width="30%"><a href="javascript:;">Responder</a></th>

        <th scope="col" width="30%"><a href="javascript:;">From</a></th>

        <th scope="col" width="20%"><a href="javascript:;">Subject</a></th>

        <th scope="col" width="35%"><a href="javascript:;">Attachment</a></th>

        <th scope="col" width="10%"><a href="javascript:;">Edit</a></th>    

        <th scope="col" width="10%"><a href="javascript:;">Delete</a></th>      

    </tr>

    </tfoot>

    <tbody>

         <?php

        $sql = "SELECT * FROM $table";

        $results = $wpdb -> get_results($sql);

        ?>

    <?php if( !empty( $results ) ) : ?>

    <?php foreach( $results as $result ): ?>

        <tr>

            <td><?php echo $result -> resp_name; ?></td>             

            <td><?php echo $result -> from_email; ?></td>

            <td><?php echo $result -> subject; ?></td>

            <td><a href="<?php echo get_bloginfo('siteurl'); ?>/download.php?filename=<?php echo $result -> att_name; ?>"><?php echo $result -> att_name; ?></a></td>

          <td><a href="admin.php?page=files&action=update&id=<?php echo $result -> id; ?>">Update</a></td>

            <td><a href="admin.php?page=files&action=delete&id=<?php echo $result -> id; ?>">Delete</a></td>

        </tr>

        <?php endforeach; ?>



    <?php else: ?>

    <tr>

        <td colspan="5">No Responders</td>

    </tr>

    <?php endif; ?>

    </tbody>

  </table>

和Download.php文件

<?php
require('wp-config.php');

$file = $_GET['filename'];

$sql = "Select * From wp_paypal_responders Where att_secure = '$file'";
$qry = mysql_query($sql);
$obj = mysql_fetch_object($qry);

$fileName = $obj->att_secure;

$download_dir = $_SERVER['HTTP_HOST'].'/uploads/';

$fullPath = $download_dir.$fileName;


  // Required for some browsers
  if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');


    // Determine Content Type
    switch ($ext) {
      case "pdf": $ctype="application/pdf"; break;
      case "exe": $ctype="application/octet-stream"; break;
      case "zip": $ctype="application/zip"; break;
      case "doc": $ctype="application/msword"; break;
      case "xls": $ctype="application/vnd.ms-excel"; break;
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
      case "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
      default: $ctype="application/force-download";
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type: $ctype");
    header("Content-Length: " .filesize($fullPath) );
    header('Content-Disposition: attachment; filename="'.basename($fullPath).'"');
    header("Content-Transfer-Encoding: binary");

    readfile( $fullPath );
    fopen($fullPath);
?>

0 个答案:

没有答案