dongmen9517 2015-09-17 18:41
浏览 40

在PHP脚本中删除包含文件

I have a php file to handle file downloads in a website. It is working as it should.

if($_GET['type'] == "pdf") {
    $dir = __DIR__ . "/../src/files/pdf/" . $_GET['name'];
    if(file_exists($dir)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename='.basename($dir));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($dir));
        readfile($dir);
        exit;
    } else {
        $_SESSION['err'] = "No file found";
        header("location: index.php");
        exit;
    }
}

Now I want to count the times that a file is being downloaded. So I have a method inside a class in a different php file. I usually include this php file and create an instance of this class when I need to use some of its methods. The problem is that if I include the file in my file-handler.php the header will be different and the file would be corrupted when downloaded. So, how can I use the method of this file in the file-handler without affecting the header?

  • 写回答

1条回答 默认 最新

  • donglian7879 2015-09-17 19:28
    关注

    If you include your counting file before at the top of file-hander.php file before you output your download headers then the download headers will replace whatever headers were output before.

    Additionally. your counting file must not output any content at all as the client is expecting the file you are downloading and nothing else.

    I agree 100% with @rjdown, your code creates a massive potential security issue. I would not put it on a server connected to an untrusted network (Like The Internet). If you would appreciate help making it secure then please ask that question.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向