Correct way to trigger a file download (in PHP)?

18,219

Solution 1

EDIT

Yes javascript, something like:

<a href="download.php" onclick="this.innerHTML='Downloading..'; downloadPdf(this);">Download</a>

If you need to actually understand when the download is started you probably need to call an iframe and then use the "onload" event on it.. for example:

// javascript
function downloadPdf(el) {
    var iframe = document.createElement("iframe");
    iframe.src = "download.php";
    iframe.onload = function() {
        // iframe has finished loading, download has started
        el.innerHTML = "Download";
    }
    iframe.style.display = "none";
    document.body.appendChild(iframe);
}

Solution 2

The solution you have for download is fine. You may want to consider some visual feedback to the user, perhaps by using javascript to show a "Downloading, please wait message" on the current page when the link is clicked via an onclick handler. Or simply indicate that the download may take some time to start next to the link. Since IE will unload the page, stopping any GIF animations, I prefer text indications for file downloads.

Solution 3

fake it by using an onclick event handler to show a spinning gif

<a href="download.php" onclick="ShowDownloading();">Download</a>

Solution 4

Automatically starting downloads usually use a meta tag inside a normal page:

<META HTTP-EQUIV="REFRESH" CONTENT="10.0;URL=download.php">

This example will redirect the browser in 10 seconds to download.php.

Share:
18,219
Lordn__n
Author by

Lordn__n

I am a software developer from Perth, Western Australia (now living in New York City) with roughly 13 years experience in developing end-to-end solutions in Java, C#, C, C++, Perl, PHP and HTML/CSS/Javascript. I have experience in developing user interfaces (Web and desktop), database design and development (Oracle, SQL Server, MySQL), data modelling, software design, data conversion, Web application development and high-performance high-availability computing. I work for Google in New York City. I have a blog I call C for Coding. Come check it out!

Updated on June 16, 2022

Comments

  • Lordn__n
    Lordn__n almost 2 years

    Quick (and hopefully easy) question: I need to trigger a download of a PDF file that's generated by a PHP file. I can do this:

    <a href="download.php">Download</a>
    

    but should I be doing this another way? Javascript maybe? The above works but the window shows "Loading..." until the download starts. I'd like to provide some feedback to the user that something is happening.

    Ideas?

    Note: I already have the code that sends the file from the server. It works perfectly. This question is simply about how best to call that script from the client.

    Some sites have downloads that automatically start. How do they do that?

    The problem with a direct URL is that if the PHP script errors it'll replace th econtent of the existing page, which is not what I want.

  • Gumbo
    Gumbo about 15 years
    You shoud return false to disable the link.