How to download a file on clicking thefile path using PHP-Symfony?

99封情书 提交于 2019-12-23 05:58:11

问题


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.

How can i do this?

edit 1:

Model - Blog_user Module - post

Table Structre - table name- 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.

edit 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

edit 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. 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 table - user_id , file(uploaded file)

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 table - post_id, blog_id, user_id


回答1:


Assuming:

  • your module name is moduleName
  • the model with the file is BlogUser
  • the primary key of the model is 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):

  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;
  }



回答2:


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;
}


来源:https://stackoverflow.com/questions/10894166/how-to-download-a-file-on-clicking-thefile-path-using-php-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!