Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 受保护的文件下载脚本。快到了_Php - Fatal编程技术网

Php 受保护的文件下载脚本。快到了

Php 受保护的文件下载脚本。快到了,php,Php,我有一个文件,用户将通过paypal、clickbank和paydotcom购买。我必须在我的服务器上托管文件的下载页面 我已将该文件放置在我的公共html文件夹外的目录中。该文件夹与public_html处于同一级别,例如称为“下载” 下面的脚本应该这样做,但我有两个问题 1) 看起来不太安全。只需在querystring上检查支付确认令牌 2) 如果路径中不包含我的site.com公用文件夹,则无法使用$path变量指向下载文件夹。例如,当我回显$path时,我得到 /home/myuser

我有一个文件,用户将通过paypal、clickbank和paydotcom购买。我必须在我的服务器上托管文件的下载页面

我已将该文件放置在我的公共html文件夹外的目录中。该文件夹与public_html处于同一级别,例如称为“下载”

下面的脚本应该这样做,但我有两个问题

1) 看起来不太安全。只需在querystring上检查支付确认令牌

2) 如果路径中不包含我的site.com公用文件夹,则无法使用$path变量指向下载文件夹。例如,当我回显$path时,我得到

/home/myuser/public_html/mysite.com
但我需要它来下定决心

/home/myuser/download/myprotectedfile.zip
我相信有一个更安全或更聪明的方法来做到这一点,所以我要求

<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>

我在让它工作时遇到的问题是$path的值包含我的site.com引用,但下载目录在site.com之外。我需要获得一个更高级别的引用,以便指向保存下载文件的目录

另外,正如我前面所说的,我不知道如何做到这一点(除了以安全的方式检查预期的querystring值)


提前谢谢

>你只需要考虑路径,就像一个普通的目录路径,而不是Web路径。因此,只需简单地提高一个级别即可

如果你是这样的话

/path2file/inhere.pdf /public_html/download.php


文件的路径应该是download.php“./path2file/inhere.pdf”

如果
public\u html
是您的文档根目录,那么您应该能够获得下载路径


realpath($\u SERVER[DOCUMENT\u ROOT]./../download')

您可以在
$path
中使用父目录快捷方式
。/
,或者使用以下功能:

$parent_dir = dirname( dirname( __FILE__ ) );
// first dirname is the directory of this file, second goes up one level, etc.

顺便说一句,注意不要在URL中指明路径,可以通过将其更改为
download.php?download\u file=../../private/bank\u certificate.pem
来读取其他文件(如配置文件或其他私有文件)。您应该使用获取文件的绝对路径,并将其与“授权下载”文件列表进行比较。

如今,对于文件下载脚本来说,自定义编码可能不是最好的解决方案。查看Drupal,它有文件下载模块,也可以与Clickbank模块集成:

与其使用循环将文件读入缓冲区,不如使用readfile函数?好的,我可以用它来获取指向存储文件的目录的指针。我只是不确定我剩下的代码现在是否正确。我不断收到一个错误,说明“警告:无法修改标题信息-标题已由…发送”1。注意
dirname
以字符串形式返回路径,而不是目录2上的文件描述符。此警告表示您已经发送了一些输出。如果
echo
“删除了某些内容,或者php文件的开头/结尾有空行或空格(如另一个包含的文件中的
之前),则可能会发生此情况。警告。我发现这个脚本有一个安全问题。如果有人提供了安全文件的路径,脚本允许下载它。示例:如果下载_file=../includes/dbconnect.php,则可能会丢失数据库密码。线程$download_文件,然后将其传递给脚本。是的,这正是我在代码示例下面写的段落的要点。