g v*_*yak 3 php zend-framework2
目前我正在研究zf2.现在我必须提供下载选项来下载pdf files.i已将所有pdf文件存储在data目录中.如何指定.phtml文件中的pdf文件的链接?
提前致谢.
用户永远不会直接访问您的/data目录.这不会那么好.但是,您可以轻松地为自己写一个download-script.php或者喜欢将这个目录的内容分发给您的用户.
如果您查看前六行,public/index.php您会看到以下内容:
<?php
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
Run Code Online (Sandbox Code Playgroud)
考虑到这一点,您知道从PHP的角度来看,访问data目录中的任何内容都非常简单data/file.pdf
你总是想给自己写一些下载记录器.给自己写一个控制器.在该控制器内部有一个动作可能被称为类似download或类似的东西.该动作应该有一个参数filename.
此操作所做的就是检查文件名是否存在file_exists('data/'.$filename)以及是否存在,只需将此文件传递给用户即可.一个示例混合或zf2和本机PHP可能是:
public function downloadAction()
{
$filename = str_replace('..', '', $this->params('filename'));
$file = 'data/' . $filename;
if (false === file_exists($file)) {
return $this->redirect('routename-file-does-not-exist');
}
$filetype = finfo_file($file);
header("Content-Type: {$filetype}");
header("Content-Disposition: attachment; filename=\"{$filename}\"");
readfile($file);
// Do some DB or File-Increment on filename download counter
exit();
}
Run Code Online (Sandbox Code Playgroud)
这不是干净的ZF2,但我现在很懒.使用适当的ResponseObject并在那里进行文件处理可能更加理想!
重要更新这件事实际上也是非常不安全的.您需要禁止父文件夹.你不希望这个人在data/download 目录之外做一些事情
`http://domain.com/download/../config/autoload/db.local.php`
Run Code Online (Sandbox Code Playgroud)
如果我没有完全弄错,只需更换所有出现的双点就足够了......