【问题标题】:How to return file in PHP without real URL? [duplicate]如何在没有真实 URL 的情况下在 PHP 中返回文件? [复制]
【发布时间】:2020-01-09 12:44:47
【问题描述】:

我这里有图片文件

/var/www/img/timetable.jpeg

网址是

www.mydomain.com/img/timetable.jpeg

但我不希望用户知道真实的 URL,我希望用户像这样访问文件

www.mydomain.com/get/timetable.jpeg

所以我使用 PHP 框架来拦截请求,例如无脂肪

$f3->route('GET /get/@filename',
    function($f3) {
        $filename=$f3->get('PARAMS.filename');
        // here return/redirect the file
    }
);

但我不想用这种方式。

$f3->route('GET /get/@filename',
    function($f3) {
        $filename=$f3->get('PARAMS.filename');
        $attachment_location = 'img/'.$filename;

        header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public");
        header("Content-Type: image/jpeg");
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length:".filesize($attachment_location));
        header("Content-Disposition: attachment; filename=".$filename);
        readfile($attachment_location);
    }
);

我期待这样的事情,但这是重定向,用户会知道 URL

$f3->route('GET /get/@filename',
    function($f3) {
        $filename=$f3->get('PARAMS.filename');

        header("Location: img/".$filename);
    }
);

我可以在 PHP 中执行此操作吗,是否调用 Forward

【问题讨论】:

标签: php http


【解决方案1】:

也许这有帮助:

function showImage($name){
    $file = basename($name);
    $path  = 'img/'.$file;
    $mime = mime_content_type($path);
    $types = [ 'gif'=> 'image/gif', 'png'=> 'image/png', 'jpeg'=> 'image/jpeg', 'jpg'=> 'image/jpeg'];
    // if allowed type
    if(in_array($mime, $types)){        
        if(file_exists($path)){
            header('Content-type: '.$mime);
            header("Content-Length: " . filesize($path));
            //echo file_get_contents($path);           
            readfile($path);
        }       
    }
}

【讨论】:

    【解决方案2】:

    您可以使用几种不同的解决方案来实现这一点。 最简单的是使用 Apache 的 .htaccess 重写规则。对图像发出的所有请求都会发送到处理请求的 image-server.php 文件。

    /get/.htaccess:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule (.*) image-server.php?file=$1 [QSA,L]
    </IfModule>
    

    不要在同一目录中托管图像。您的 image-server.php 将读取 $_GET["file"] 值并从受保护的目录返回图像。

    <?php
    # /get/image-server.php
    
    header("Content-Type: image/jpeg");
    $secret_location = "/var/www/html/images/timetable.jpeg";
    readfile($secret_location);
    

    这是一个基本的使用示例。您应该检查 $_GET["file"],即。 $secret_location="/PATH/{$_GET['file']}";确保图片文件存在,然后readfile()

    所以,你的目录结构是:

    /get/.htaccess
    /get/image-server.php
    /secret-location/images/timetable.jpeg
    

    您的所有图片都没有真实的 URL,因为您将在 htdocspublic_html 文件夹之外托管图片。

    【讨论】:

    • 我不明白,.htaccess 文件只是阻止用户访问 img 文件夹,但这不是问题,因为我不会公开图像文件夹名称,用户不知道 img 文件夹姓名。我还使用 .htaccess 将 url 重写为 www.mydomain.com/get/xxxxx。我的问题是如何在www.mydomain.com/get/xxxxx 中返回文件
    • 我编辑了答案以提及您的 image-server.php 的全部内容。
    • 我在我的问题中写了同样的东西,我不想使用它。 X-Sendfile 正是我想要的解决方案
    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2014-11-11
    • 2020-10-19
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2018-09-15
    相关资源
    最近更新 更多