0

tcpdf を使用して pdf レポートを生成しています。そのレポート ファイルを Web ルート ディレクトリに保存しました。次に、そのページにリダイレクトしてpdfを表示しています

コード: すべて選択 http://sitename/pdf/student_record.pdf

ディレクトリ名pdfとファイル名を開示したくありません。セキュリティ上の理由から、重複した名前を設定したいと思います。routing.yml を使用してそれを行うにはどうすればよいですか?

ありがとうございました。

4

3 に答える 3

3

私はあなたがしたいように何かをしなければなりませんでした。そこで、sfActions を myActions で拡張し、メソッドを追加しました。

/**
 *
 * @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
于 2011-04-08T08:47:04.780 に答える
0

symfonyを使用してファイルをアップロードしないでください。ただし、Webサーバーは直接アップロードできます。たとえば、ファイルを/ path / to / pdfなどの外部ディレクトリに保存してから、仮想ホストに次を追加します。

Alias /pdf /path/to/pdf

名前も変更したい場合は、ある種の書き換えルールを使用できます。

于 2011-04-08T09:33:36.830 に答える
0

fpassthru()pdf を webroot の外に保存し、ユーザーがアクセスできるかどうかを確認するルートとアクションを作成し、pdf ファイルに正しいヘッダーを設定して、またはを介し​​て出力しますreadfile()

于 2011-04-08T08:45:36.887 に答える