0

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

Community
  • 1
  • 1
fdz
  • 73
  • 2
  • 22
  • Are you using the admin generator for the admin view? And btw, symfony 1.4 or 2.0? – j0k Jun 05 '12 at 09:13
  • no im not using admin generator.. im using symfony 1.4.16 – fdz Jun 05 '12 at 09:37
  • You don't have an id for the table? What is the primary key of the blog_user table? – j0k Jun 05 '12 at 11:49
  • Could you show us and exemple of `file` from your db? – j0k Jun 05 '12 at 12:01
  • E:\xampp\htdocs\trunk\web\uploads/post_upload/post_Uploaded -JS.pdf this is how it save inside db – fdz Jun 05 '12 at 15:19
  • Can't you save only the filename, like `post_Uploaded -JS.pdf` ? It will be easier to retrieve them, and it's not a god idea to store the absolute path to the file since it depends on the machine (here you local machine). – j0k Jun 05 '12 at 15:39
  • yes, i also want to save it like that but i dont know how to do it .. i've mention the save method above edited space.can you please go through it and let me know if there any mistakes the way i have used there? – fdz Jun 05 '12 at 15:45
  • File Save 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); } – fdz Jun 05 '12 at 15:51
  • Put it in your question instead.. – j0k Jun 05 '12 at 15:51
  • ok done.. sorry im not very familiar with stackOverflow.. thats why im always doing it wrongly when putting commnets – fdz Jun 05 '12 at 15:57
  • Do not remove all your post when editing, add the fresh information at the bottom. – j0k Jun 05 '12 at 16:36
  • But, humm, this is not the way you save the filename in db. The `saveFile` you show save the file in the filesystem, not in db. – j0k Jun 05 '12 at 16:39
  • ok , next time i'll do like that. – fdz Jun 05 '12 at 16:39
  • i didnt get that @jok. i have wrote that saveFile method inside my form .. – fdz Jun 05 '12 at 16:41
  • you mean do i have use two methods to save in db and filesystem seperatly. – fdz Jun 05 '12 at 16:42
  • update your post with the Form class of Blog_user – j0k Jun 05 '12 at 16:45
  • that mean inside addBlogForm.php right? so what do i need to update.. SaveFile method? – fdz Jun 05 '12 at 16:51
  • Sorry, when I say update your post, I mean your question :) – j0k Jun 05 '12 at 17:00
  • @j0k hey its working now.. thankyou.. now i can d/l the file :) – fdz Jun 06 '12 at 03:42

2 Answers2

0

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;
}
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • ok, on my listAllPost template there i display all the posts the i passed the paramete of each user_id and using addPost template im displaying all the details regarding to the user such as User ID, User Type, Post Title and Post. There under the post it displays the file path of that particular file which user upload. so is it posible to me to give a that path as a link and download that file? – fdz Jun 05 '12 at 08:56
  • You can call like this yourdomain.com/controller/download/filename and change the function to function download($argument) – Miqdad Ali Jun 05 '12 at 08:58
  • here what i did so far -- i create a link to path of the file , like this getPost() ?> and it output - /home/dev/myBlog/trunk/web/uploads/post_upload/post_title.pdf this is my controller Public function execute($request) { $this->jobseeker = Doctrine_Core::getTable('Jobseeker')->find(array($request->getParameter('user_id'))); } is it ok to use the method which u have metion earlier according to this? – fdz Jun 05 '12 at 09:22
  • for example I am writing this function in controller files then I am calling this like Download and i will change file path and name in download method – Miqdad Ali Jun 05 '12 at 09:26
0

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;
  }
j0k
  • 22,600
  • 28
  • 79
  • 90
  • it give me a error 404 The requested URL /frontend_dev.php/application/download/user_id//home/MyBlog/trunk/web/uploads/post_upload/post.pdf was not found on this server – fdz Jun 05 '12 at 11:17
  • Well, give me more information. Update your question with: the model name, the module name, the table structure and one content. And I will update my answer. – j0k Jun 05 '12 at 11:19
  • Model - Blog_user = Application Module - post -------------------- Table Structre - table name- Blog_user user_id bigint(20) gender varchar(255) blog_status tinyint(1) file varchar(255) – fdz Jun 05 '12 at 11:35
  • @farah better edit your answer and put these informations inside, it will be more easy to read it. – j0k Jun 05 '12 at 11:36
  • Form - 'user_id' => new sfWidgetFormInputHidden(), 'gender' => new sfWidgetFormInputText(), 'file' => new sfWidgetFormInputFile(), – fdz Jun 05 '12 at 11:36
  • can u please tell how to structre the comment..why it always display as paragraph? – fdz Jun 05 '12 at 11:37
  • If you want to provide more information, like code, structure, etc .. [edit your answer](http://stackoverflow.com/posts/10894166/edit). You can't format the comment using paragraph. – j0k Jun 05 '12 at 11:38
  • There is one more thing that i have to clarify with you @j0k 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? – fdz Jun 06 '12 at 05:54
  • Well you will need to retrieve the `blog_id` and put it in a hidden field. But I don't know how to do (a bit complex without the code, the project, etc ..) – j0k Jun 06 '12 at 07:27