在 symfony 中隐藏直接文件 URL

发布于 2024-10-31 13:12:19 字数 282 浏览 4 评论 0原文

我正在使用 tcpdf 生成 pdf 报告。我已将该报告文件存储在 Web 根目录下。然后我通过重定向到该页面来显示 pdf,如

代码:全选 http://sitename/pdf/student_record.pdf

我不想透露目录名 pdf 和文件名。由于某些安全原因,我想为此设置一些重复的名称。我如何使用routing.yml 来做到这一点?

谢谢。

I'm using tcpdf to generate pdf report. I have stored that report files under web root directory. Then I am displaying pdf by redirecting to that page like

Code: Select all
http://sitename/pdf/student_record.pdf

I dont want to disclose the directory name pdf and filename. I want to set some duplicate name for that because of some security reasons. How can I do that using routing.yml?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残龙傲雪 2024-11-07 13:12:19

我必须做一些像你想做的事情。
因此,我使用 myActions 扩展了 sfActions 并添加了方法:

/**
 *
 * @param string $path
 * @param string $mimetype
 * @return sfView::NONE
 */
public function downloadFile($path, $mimetype, sfRequest $request) {
    if (is_file($path) && is_readable($path)) {
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($path) . '"');
        $this->getResponse()->setHttpHeader('Content-type', $mimetype);
        $this->getResponse()->setHttpHeader('Pragma: public', true);
        $this->getResponse()->setHttpHeader('Expires', 0);
        $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
        $this->getResponse()->setHttpHeader('Content-Length', filesize($path));
        $this->getResponse()->sendHttpHeaders();
        file_put_contents(sfConfig::get('sf_upload_dir').'/test.txt', var_export($_SERVER, 1) . 'TEST');
        if($request->isMethod(sfRequest::HEAD))
            $this->getResponse()->setContent('');
        else
            $this->getResponse()->setContent(file_get_contents($path));
    }
    else
        $this->forward404('Unreadable file', 'json');

    return sfView::NONE;
}

并将该方法用于路由操作。例如,我们有模型 Author 和 mp3。因此,在routing.yml中:

author_show:
  url:       /api/authors/:id.:sf_format
  class:     sfDoctrineRoute
  options:   { model: Author, type: object }
  param:     { module: author, action show }
  requirements:
    sf_format: (?:mp3)

实际操作:

switch($request->getParameter('sf_format')) {
        case 'mp3':
            $path = sfConfig::get('sf_upload_dir') . '/authors/audio/' . $author->getAboutAudio();
            $mimetype = 'audio/mpeg';
            break;

        default:
            $this->forward500('Unknown format. Only mp3 format is available.', 'json');
    }
    return $this->downloadFile($path, $mimetype, $request);

此外,您可以将其存储在上传中的某个子文件夹中,并使用.htaccess保护它,例如

deny from all

I had to do something like you would like to have.
So, I extended sfActions with myActions and add method:

/**
 *
 * @param string $path
 * @param string $mimetype
 * @return sfView::NONE
 */
public function downloadFile($path, $mimetype, sfRequest $request) {
    if (is_file($path) && is_readable($path)) {
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($path) . '"');
        $this->getResponse()->setHttpHeader('Content-type', $mimetype);
        $this->getResponse()->setHttpHeader('Pragma: public', true);
        $this->getResponse()->setHttpHeader('Expires', 0);
        $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
        $this->getResponse()->setHttpHeader('Content-Length', filesize($path));
        $this->getResponse()->sendHttpHeaders();
        file_put_contents(sfConfig::get('sf_upload_dir').'/test.txt', var_export($_SERVER, 1) . 'TEST');
        if($request->isMethod(sfRequest::HEAD))
            $this->getResponse()->setContent('');
        else
            $this->getResponse()->setContent(file_get_contents($path));
    }
    else
        $this->forward404('Unreadable file', 'json');

    return sfView::NONE;
}

and used that method for actions from routing. For example, we have model Author and mp3 for it. So, in routing.yml:

author_show:
  url:       /api/authors/:id.:sf_format
  class:     sfDoctrineRoute
  options:   { model: Author, type: object }
  param:     { module: author, action show }
  requirements:
    sf_format: (?:mp3)

In action:

switch($request->getParameter('sf_format')) {
        case 'mp3':
            $path = sfConfig::get('sf_upload_dir') . '/authors/audio/' . $author->getAboutAudio();
            $mimetype = 'audio/mpeg';
            break;

        default:
            $this->forward500('Unknown format. Only mp3 format is available.', 'json');
    }
    return $this->downloadFile($path, $mimetype, $request);

Also, you can store it in some subfolder in uploads and secure it with .htaccess, like

deny from all
赢得她心 2024-11-07 13:12:19

将 pdf 保存在 webroot 之外,然后创建一个路由和操作来检查用户是否可以访问它,然后为 pdf 文件设置正确的标头并通过 fpassthru()输出它读取文件()。

Save the pdf outside of the webroot, then create a route and action that checks if the user may access it, then sets the correct headers for a pdf file and outputs it via fpassthru() or readfile().

那一片橙海, 2024-11-07 13:12:19

您不应该使用 symfony 上传文件,而您的 Web 服务器可以直接执行此操作。例如,将文件保存在外部目录中,例如 /path/to/pdf,然后在虚拟主机中添加:

Alias /pdf /path/to/pdf

如果您还想更改名称,则可以使用某种重写规则。

You should'nt use symfony to upload files, while your web server can do it directly. For exemple, save your files in an external directory, like /path/to/pdf, then, in your virtualhost, add :

Alias /pdf /path/to/pdf

If you also want to change the name, you could use some kind of rewrite rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文