单击时,PHP文件下载脚本不起作用。但是在浏览器

时间:2016-07-13 05:10:36

标签: php

我遇到了一个奇怪的问题。我有一个下载zip文件的PHP代码。单击链接时,将从另一个php页面调用该代码。该链接传递4个参数。第一个参数是file1,第二个是file2,第三个是数据库中的id,第四个是位置代码。这在我的一个页面中完美运行。但是当我尝试使用不同的参数从另一个页面调用它时,脚本失败了。检查文件目录权限和所有权,一切正常。我试图下载的文件就在那里。当我尝试直接访问该链接时,它会下载该文件。很奇怪......这里是download.php

if(isset($_GET['download_file'])&&isset($_GET['download_file2'])&&isset($_GET['id'])&&isset($_GET['loc'])){
    $id=$_GET['id'];
    $loc=$_GET['loc'];
    switch ($loc) {
        case 1:
            $path = "../uploads/b2ho/";
            break;
        case 2:
            $path = "../uploads/ho2b/";
            break;
        default:
            exit;
    }
    $dt_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']);
    $dt_file = filter_var($dt_file, FILTER_SANITIZE_URL);
    $fd_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file2']);
    $fd_file = filter_var($fd_file, FILTER_SANITIZE_URL);
    $dt_fullPath = $path.$dt_file;
    $fd_fullpath = $path.$fd_file;
    $zipname = 'BISAR.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    $zip->addFile($dt_fullPath,$dt_file);
    $zip->addFile($fd_fullpath,$fd_file);
    $zip->close();

    if (file_exists($zipname)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($zipname));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($zipname));
        ob_clean();
        flush();
        readfile($zipname);
        exit;
        $x=true;
    }

    if($loc==1){
        if($x==true){
            $stmt = $conn->prepare("UPDATE rfc_bisar_b2h_upl_log SET bisar_proc_status='Pending',bisar_proc_down_date='$date',bisar_down_by='$userid' where bisar_upl_id=?");
            $stmt->bind_param('s',$id);
            $stmt->execute();
            mysqli_close($conn);
            header("Refresh:0");
        }
    }
    if($loc==3){
        if($x==true){
            $stmt = $conn->prepare("UPDATE rfc_bisar_h2b_upl_log SET bisar_proc_status='Pending',bisar_proc_down_date='$date',bisar_down_by='$userid' where bisar_upl_id=?");
            $stmt->bind_param('s',$id);
            $stmt->execute();
            mysqli_close($conn);
            header("Refresh:0");
        }
    }
    if(file_exists($zipname)){
        unlink($zipname);
    }
}

3 个答案:

答案 0 :(得分:0)

尝试检查损坏的链接参数。我认为该链接无效(查询字符串参数之一具有无效名称并且isset条件未通过)

答案 1 :(得分:0)

  1. $zipname = 'BISAR'.zip';更改为$zipname = 'BISAR.zip';
  2. 在顶部,您有位置1和2,位于底部1和3
  3. 如果是
  4. ,请使用caseelse代替底部位置
  5. 注意:未定义的变量:date
  6. 未定义的变量:userid
  7. 调用未定义的方法PDOStatement::bind_param()应为bindParam
  8. where bisar_upl_id=?更改为where bisar_upl_id=:bisar_upl_id
  9. $stmt->bindParam('s ',$id)更改为$stmt->bindParam(':bisar_upl_id',$id)
  10. mysqli_close()期望参数1为mysqli,给定对象。应该是$ stmt-> closeCursor();但这甚至不是必需的
  11. 数据库表创建语句:

    CREATE TABLE `rfc_bisar_b2h_upl_log` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `bisar_proc_status` varchar(45) DEFAULT NULL,
      `bisar_proc_down_date` varchar(45) DEFAULT NULL,
      `bisar_down_by` varchar(45) DEFAULT NULL,
      `bisar_upl_id` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
    

    数据库表填充:

    INSERT INTO `rfc_bisar_b2h_upl_log` (`id`, `bisar_upl_id`) VALUES ('1', '1');
    

    ,这里正在使用php代码:

        $_GET['download_file'] = 'xxxx.php';
        $_GET['download_file2'] = 'xxxx2.php';
        $_GET['id'] = 1;
        $_GET['loc'] = 1;
        $date = 1;
        $userid = 1;
    
       ini_set('display_errors', 1);
       ini_set('display_startup_errors', 1);
       error_reporting(E_ALL);
    
    
        if (isset($_GET['download_file']) && isset($_GET['download_file2']) && isset($_GET['id'])){
    
            $id  = $_GET['id'];
            $loc = $_GET['loc'];
            switch ($loc) {
                case 1:
                    $path = "../uploads/b2ho/";
                    break;
                case 2:
                    $path = "../uploads/ho2b/";
                    break;
                default:
                    die('wrong location passed');
            }
            $dt_file     = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']);
            $dt_file     = filter_var($dt_file, FILTER_SANITIZE_URL);
            $fd_file     = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file2']);
            $fd_file     = filter_var($fd_file, FILTER_SANITIZE_URL);
            $dt_fullPath = $path.$dt_file;
            $fd_fullpath = $path.$fd_file;
            $zipname = 'BISAR.zip';
            $zip = new ZipArchive;
            $zip->open($zipname, ZipArchive::CREATE);
            $zip->addFile($dt_fullPath,$dt_file);
            $zip->addFile($fd_fullpath,$fd_file);
            $zip->close();
    
            if (file_exists($zipname)) {
    
                $quoted = sprintf('"%s"', addcslashes(basename($zipname), '"\\'));
                $size   = filesize($zipname);
    
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename=' . $quoted);
                header('Content-Transfer-Encoding: binary');
                header('Connection: Keep-Alive');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . $size);
                flush();
                readfile($zipname);
                $x=true;
            }
    
        if($loc==1){
            if($x==true){
                $stmt = $conn->prepare("UPDATE rfc_bisar_b2h_upl_log SET bisar_proc_status='Pending',bisar_proc_down_date='$date',bisar_down_by='$userid' where bisar_upl_id=:bisar_upl_id");
                $stmt->bindParam(':bisar_upl_id',$id);
                $stmt->execute();
                header("Refresh:0");
            }
        }else{
            if($loc==2){
                if($x==true){
                    $stmt = $conn->prepare("UPDATE rfc_bisar_b2h_upl_log SET bisar_proc_status='Pending',bisar_proc_down_date='$date',bisar_down_by='$userid' where bisar_upl_id=:bisar_upl_id");
                    $stmt->bindParam(':bisar_upl_id',$id);
                    $stmt->execute();
                    header("Refresh:0");
                }
            }
    
        }
        if(file_exists($zipname)){
            unlink($zipname);
        }
    }
    

答案 2 :(得分:0)

感谢所有帮助人员。通过添加target =" _ blank"解决了这个问题。在我的锚标签上,我称之为download.php。真的很奇怪为什么php不允许我在同一页面上下载文件,而是要求我打开一个新标签。