Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
File::download() keeps on giving :)
  • I have 2 links on my page, one that downloads a file and the other emails the file. I use the FuelPHP \File class to download the file. If I click the download link first (which works as expected) and then the email link, I get asked if I want to download a file again, although the email still sends. I assume this is some sort of header issue with the page still thinking it's a PDF, but is there a way to fix this. I'm sure it's simple but I'm just missing something. I've tried redirecting the page, but since the \File::download() method exits after completion the function never reaches the redirect. Thanks for the help!
  • I can't comment without seeing how you have implemented it, but these two functions should be different URL's. If not, it might be a browser cache issue.
  • They're definitely two different urls. One is http://domain.com/email/id and the other is http://domain.com/download/id. Here is the (stripped down) code for both: Download:
    public function action_download($id = null)
     {
      if($id == null) exit;
    
      if(file_exists(APPPATH . 'path/to/file.pdf'))
      \File::download(APPPATH . 'path/to/file.pdf');
     }
    

    Email:
    public function action_email($id = null)
     {
      if($id == null) exit;
      $data = //some data
    
      // Create an instance
      $email = \Email::forge();
    
      // Set the from address
      $email->from('person@domain.com', 'From Me');
    
      $email_to = // DB query to get the email recipients as an array
    
      $emails = array();
      foreach($email_to as $to)
      {
       $emails[$to['email']] = $to['first_name'] . ' ' . $to['last_name'];
      }
    
      // Set a subject
      $email->subject('Subject');
    
      // Set multiple to addresses
      $email->to($emails);
    
      // And set the body.
      $email->html_body(\View::forge('module/email', $data));
    
      if(file_exists(APPPATH . 'path/to/file.pdf'))
       $email->attach(APPPATH . 'path/to/file.pdf');
    
      try
      {
          $email->send();
      }
      catch(\EmailValidationFailedException $e)
      {
          // The validation failed
      }
      catch(\EmailSendingFailedException $e)
      {
          // The driver could not send the email
      }
    
      if( ! \Input::is_ajax())
      {
       \Session::set_flash('success', 'Emails successfully sent!');
       \Response::redirect(\Session::get('prevurl'));
      }
     }
    

    Also, I just checked on a browser (chrome) that hasn't seen this site yet and it did the same thing. I think we could rule out a browser cache issue. I appreciate your help!
  • These URI's don't map to the actions, so I assume you have some custom routing in place? I don't see any reason why a call to action_email() would trigger a download. These are standard links in your view? Or are they triggered by an ajax call? What happens if you request the email URL directly in the browser, without clicking on a link?
  • Sorry, I didn't put the full url; its http://domain.com/module/controller/action/id. There are no custom routes. There are no ajax calls at the moment, its just setup for later use. Accessing the email url directly works great. It also works as expected if I click the email link before I click the download link; which leads me to think that there's some artifact left over by the download. I just can't figure out what. Even when the email links asks if I want to download it still sends the email as expected. Its strange.
  • Sorry, I think this requires major debugging, I don't have a clue why this is happening...
  • Man! If feel like an idiot now! I figured out the problem and it was so simple. The problem was this line of code:
    if ( ! \Input::is_ajax())
    {
        \Session::set_flash('success', 'Emails successfully sent!');
        \Response::redirect(\Session::get('prevurl'));
    }
    
    I use the shutdown event to save the previous url in a session. So, of course, when I visit the download link it saves that url, then when I click the email url it it tries to redirect back to the download page since that was the last url visited. Looks like I'll just need to write a check in the shutdown event so that it doesn't capture anything from the download action. Thanks for taking the time to help!
  • lol. If only all issues where this simple. Glad you've got it sorted!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion