Home php How to switch to a new page and download the yii2 file...

How to switch to a new page and download the yii2 file at the same time?




My code snippet, but if the first if is not executed, the transition to the desired page does not occur, but only the download of the file. How do I get around this?

if ($ this- & gt; flag == 1) {
        Yii :: $ app- & gt; mailer- & gt; compose ()
          - & gt; setTo ($ this- & gt; email)
          - & gt; setFrom (['@ gmail.com' = & gt; Yii :: $ app- & gt; params ['senderName']])
          - & gt; setReplyTo ([$ this- & gt; email = & gt; $ this- & gt; fromPerson])
          - & gt; setSubject ("Your envelope")
          - & gt; attachContent ($ pdf- & gt; Output ('', 'S'), ['fileName' = & gt; 'pdfName.pdf', 'contentType' = & gt; 'application / pdf'])
          - & gt; send ();
      header ('Content-Type: application / octet-stream');
      header ('Content-Disposition: attachment; filename ='. 'enelope.pdf');
      exit ($ pdf- & gt; Output ('', 'S')); // does not work correctly, does not translate to the desired page

Answer 1

It is impossible to give the user a file and go to another URL at the same time. At least within one browser window or one tab. If you still need it, you have only one way out – you go to a new page and in its headers set the meta tag refresh with a link to the downloaded file and the time after which you need to start downloading the file.

& lt; meta http-equiv = "refresh" content = "5; url = https: //example.com /download/myfile.pdf">

In this example, after loading the page, the download of the file myfile.pdf

will start in 5 seconds

This solution may not work if for some reason you do not want the files to be in the public domain and anyone can download them and therefore the file is outside the site root and is given only through php -file that has access to this file locally.

Although this, in principle, can be circumvented. For each such file, you create a unique link which, in addition to everything, has a lifetime , after which it is deleted. For example, all links will be of the form https://example.com/download/<md5 (from the current time + user id) / & lt; filename & gt; You send all hits like https: //example.com/download/</S+ 47,48>/(\S+) to a separate php file. It gets a unique link from this URL checks that it exists in the DB , understands which user this link belongs to, gets the file name from the URL , checks that it is this link that is associated with this name and this user, checks that the lifetime has not expired – gives the file to the user to download. Well, you can also, as soon as someone crawls into the DB for this information, automatically delete links with an expired lifetime from the database, or mark them as deleted.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions