如何使用 PHP-Symfony 单击文件路径下载文件? [英] How to download a file on clicking thefile path using PHP-Symfony?
问题描述
我正在使用 symfony 创建一个用于博客的网站.用户可以将他们的帖子上传到网站.当用户添加文件时,它将保存在 web/upload/file_upload
中,文件路径将保存在 add_post
表中.当管理员查看 add_post
表模板时,他可以看到每个用户的下载文件的路径,我想要做的是通过这个文件路径下载文件.
I'm creating a website using symfony for blogging. Users can upload their posts to the site. when a user add a file it will save inside web/upload/file_upload
and the file path will save in add_post
table. When a admin view a add_post
table template he can see the path of the downloaded file of each and every user, what i want to do is through this file path download the file.
我该怎么做?
编辑 1:
模型 - Blog_user 模块 - 帖子
Model - Blog_user Module - post
表结构 - 表名 - Blog_user
Table Structre - table name- Blog_user
1 user_id bigint(20)
2 gender varchar(255)
3 blog_status tinyint(1)
4 file varchar(255)
表格
'user_id' => new sfWidgetFormInputHidden(),
'gender' => new sfWidgetFormInputText(),
'file' => new sfWidgetFormInputFile(),
这里上传文件时,文件路径保存在Blog_user表中,文件保存在web/upload目录中.
Here when uploading a file, the filepath save in Blog_user table and file save inside web/upload directory.
编辑 2:
//保存文件方法
public function saveFile(){
$file = $this->getValue('file');
if(isset($file)){
$filename = 'POST_Uploaded -' .($file->getOriginalName());
$file->save(sfConfig::get('sf_upload_dir').'/post_upload'.'/'.$filename);
}
}
E:\xampp\htdocs\trunk\web\uploads\post_upload\POSt_Uploaded -JS.pdf
这如何保存在侧 web/upload/post_upload 目录中,相同的路径也将保存在 db 中
This how it saved in side web/upload/post_upload directory and same path will save inside db also
编辑 3:
当用户上传博客时,它会保存在 blog_user 表中,它包含 blog _id 作为主键,user_id 在 user 表中.我想要做的是当用户上传文件时, user_id 和 blog_id 都应该保存在 blog 表中.怎么办?
when a user upload a blog it will save in blog_user table and it consist blog _id as primary key, user_id is on user table. what i want do is when user upload a file , both user_id , and blog_id should be saved inside blog table . how to do it?
用户表 - user_id ,文件(上传文件)
user table - user_id , file(uploaded file)
blog table - blog_id - 有blog titles,每个title都有一个唯一的blog id,这样用户就可以在每个title下上传一个文件,
blog table - blog_id - there are blog titles , each title has an unique blog id , so that user can upload a file under each titles,
帖子表 - post_id、blog_id、user_id
post table - post_id, blog_id, user_id
推荐答案
假设:
- 你的模块名称是
moduleName
- 带有文件的模型是
BlogUser
- 模型的主键是
id
我会走这条路:
在您的模板中:
<a href="<?php echo url_for('post/download?user_id='.$blog_user->getUserId()) ?>">Download file</a>
然后,在您的操作中(使用 Miqdad Ali 中的功能):
Then, in your action (use the function from Miqdad Ali):
public function executeDownload(sfwebRequest $request)
{
$blog_user = Doctrine_Core::getTable('Blog_user')->find($request->getParameter('user_id'));
$this->forward404Unless($blog_user);
header('content-type:');
header('Content-Description: File Transfer');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($blog_user->getFile()));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($blog_user->getFile()));
ob_clean();
flush();
readfile($blog_user->getFile());
return sfView::NONE;
}
这篇关于如何使用 PHP-Symfony 单击文件路径下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!