Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

How to make a button to start a configured file download in PHP?

Hi Experts!

I'm facing difficulties to make a form with a button to fire a download.
This form must send a parametrized name of a file to be downloaded.

            ....
               <tr>
                    <td class="tg-yw4l">
                    <!-- The point where I intend to fire the download  -->
                   <form action="download_pdf_publicacoes.php" method="post">

                  // This is the file to be downloaded:
                  <?php "base_url("."cesanshome/carregando_arquivo/".$idpublicacoes.")" ?>

                    // Here:  How to configure it to send to the other page?

                    <input type="submit" name="download_pdf" value="PDF"/>
 

                   </form>
                    </td>
              </tr>

...

Open in new window



The download_pdf_publicacoes.php
 <?php

                // How the $filename name must be received here?

	$post = filter_input(INPUT_POST,"download_pdf");
 

    if ($post)
	{
		$filename = ???;
		header("Content-Type: application/octet-stream");
		header("Content-Length: " . filesize($filename));
		header("Content-Disposition: attachment; filename=" . basename($filename));
		readfile($filename);
	}
?>

Open in new window


Thanks in advance!
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I can show you how to force a file download.  This seems to work correctly in every browser.  Perhaps you can change the "link" to a "button" and get what you need.
<?php // demo/force_download.php

/**
 * Show how to force a file download
 *
 * Choose a file to download, either via a hard-coded file path
 * or via a URL parameter like this
 *
 * path/to/demo/force_download.php?url=path/to/file.txt
 *
 * If you wanted to use the URL parameter, you might want to
 * (1) Secure the script with password protection
 * (2) replace the $url variable with something like this:
 * $url = $_GET['url'];
 *
 * If you use the URL parameter, be careful that the script
 * does not expose sensitive data on your server!
 */
error_reporting(E_ALL);


// WHAT FILE DO WE WANT TO DOWNLOAD?
$url = "http://www.IcoNoun.com/demo/short_text_file.txt";


// USE CASE
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($url, $filename=NULL)
{
    // GET THE DOWNLOAD FILE NAME
    if (empty($filename)) $filename = basename($url);

    // GET LENGTH AND FILE RESOURCE POINTER
    $hdr = get_headers($url, TRUE);
    $len = trim($hdr['Content-Length']);
    $fpr = fopen($url,'rb');

    // ON SUCCESS
    if ($fpr)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-length: $len");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // WRITE THE FILE
        fpassthru($fpr);
    }

    // ERROR
    else
    {
        trigger_error("Unable to open $url", E_USER_ERROR);
    }
}

Open in new window

Avatar of Eduardo Fuerte

ASKER

Hello

Could you give me just one more help?

Using your function in a button.
This piece of code doesn't  work out, what's wrong and what coudl be done to fix it?

   <form action="force_download(<?php echo base_url("cesanshome/carregando_arquivo/$idpublicacoes)"); ?>" method="post">
                        <input type="submit" value="PDF"/>
                    </form>

Open in new window



User generated image
To make it more difficult this Project uses CodeIgniter... so the localization is much more complex.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for the assistance, Ray!
Glad to help!  Best of luck with the project, ~Ray