下载未按预期工作。我是php的初学者

下载未按预期工作。我是php的初学者,php,download,Php,Download,我有个问题。我做了一点下载代码。我得到的只是一张空白页。 下载网址: 这是我的代码: <?php if(isset($_REQUEST["file"])){ // Get parameters $images = array("header.png"); $file = urldecode($_GET["file"]); // Decode URL-encoded string if(in_array($file, $images, true)){

我有个问题。我做了一点下载代码。我得到的只是一张空白页。 下载网址: 这是我的代码:

<?php
if(isset($_REQUEST["file"])){

    // Get parameters
    $images = array("header.png");
    $file = urldecode($_GET["file"]); // Decode URL-encoded string

    if(in_array($file, $images, true)){
        $filepath = "../images/" . $file;

        // Process download
        if(file_exists($filepath)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));
            flush(); // Flush system output buffer
            readfile("$filepath");
            exit;
        }
    }
    else{
        echo "File does not exist.";
    }
}
?>
谢谢你的帮助和时间,
Jonas

您的代码运行良好;我要做的是检查../images文件夹的路径

我添加了一个新的else子句来告诉您该文件是否不存在。这将让您知道什么时候找不到该文件,因为您当前的代码完全跳过了它

<?php
if(isset($_REQUEST["file"])){
    echo 'Loading...';
    // Get parameters
    $images = array("header.png");
    $file = urldecode($_GET["file"]); // Decode URL-encoded string

    if(in_array($file, $images, true)){
        $filepath = "images/" . $file;
        // Process download
        if(file_exists($filepath)) {
            echo 'Preparing download...';
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));
            flush(); // Flush system output buffer
            readfile("$filepath");
        }
        else
        {
            echo 'File does not exist.';
        }
    }
    else{
        echo "Not authorized.";
    }
}

那么您的http服务器错误日志文件说什么呢?白色死亡屏幕:错误检查\显示关闭,打开它们查看错误。在php页面顶部添加:ini_set'display_errors'、'On';ini_设置“html_错误”,0;错误报告-1;假设启用了错误报告,如果文件_exists返回false,则此代码将为您提供一个空白页。如果这是一个有效的图像,则看起来路径错误-您不需要../假设这是您尝试下载的图像,当然他不会看到错误。。。代码很好。。。打赌这是你不想围绕这个问题编码的路径文件-这是一个一次性设置问题。这里的任务是找出真正的问题是什么。你在猜,那不是答案。我不是在猜,我运行脚本一点问题都没有。我尝试在正确的路径中添加头映像,并成功下载了该文件,因此else子句只是让他们知道找不到该文件。