[英]How to download a file on clicking thefile path using PHP-Symfony?
I'm creating a website using symfony for blogging. 我正在使用symfony创建博客网站。 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. 用户添加文件时,它将保存在web/upload/file_upload
,文件路径将保存在add_post
表中。 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. 当管理员查看add_post
表模板时,他可以看到每个用户的下载文件的路径,我要做的就是通过此文件路径下载文件。
How can i do this? 我怎样才能做到这一点?
edit 1: 编辑1:
Model - Blog_user Module - post 模型-Blog_user模块-发布
Table Structre - table name- Blog_user 表结构-表名-Blog_user
1 user_id bigint(20)
2 gender varchar(255)
3 blog_status tinyint(1)
4 file varchar(255)
Form 形成
'user_id' => new sfWidgetFormInputHidden(),
'gender' => new sfWidgetFormInputText(),
'file' => new sfWidgetFormInputFile(),
Here when uploading a file, the filepath save in Blog_user table and file save inside web/upload directory. 在这里,上传文件时,文件路径保存在Blog_user表中,文件保存在web / upload目录中。
edit 2: 编辑2:
//save file method //保存文件方法
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
This how it saved in side web/upload/post_upload directory and same path will save inside db also 这样将其保存在web / upload / post_upload目录和相同路径中的方式还将保存在db中
edit 3: 编辑3:
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. 当用户上载博客时,它将保存在blog_user表中,并且它由blog _id作为主键,user_id在user表中。 what i want do is when user upload a file , both user_id , and blog_id should be saved inside blog table . 我想要做的是当用户上传文件时,user_id和blog_id都应保存在Blog表中。 how to do it? 怎么做?
user table - user_id , file(uploaded file) 用户表-user_id,文件(上传的文件)
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, 博客表-blog_id-有博客标题,每个标题都有唯一的博客ID,以便用户可以在每个标题下上传文件,
post table - post_id, blog_id, user_id 帖子表-post_id,blog_id,user_id
You can write this file inside any controller and call that function while user clicking on the download 您可以在任何控制器中写入此文件,然后在用户单击下载文件时调用该函数
function download(){
$file = "path/to/the/file.zip";
if (file_exists($file)) {
exit;
}
header('content-type:');
header('Content-Description: File Transfer');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;
}
Assuming: 假设:
moduleName
您的模块名称是moduleName
BlogUser
该文件的模型为BlogUser
id
模型的主键是id
I will go this way: 我会这样走:
In your template: 在您的模板中:
<a href="<?php echo url_for('post/download?user_id='.$blog_user->getUserId()) ?>">Download file</a>
Then, in your action (use the function from Miqdad Ali ): 然后,在操作中(使用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.