Submit button to download a file

Hi Guys,

i was wondering if this was possible, when a user wants to download a file from my server say: filename.pdf they need to “right click” save as to get it, but i was wondering if there was a way i could have a regular submit button do it for example:

<input type="submit" name="submit" value="Download File" />

once it’s clicked it pops up the download box

thanks for any help guys

Graham

Hi Graham

set it up as a form with the filename in a hidden field, when the user submits the form it directs to the download page and servers the file specified. Useful for tracking number of downloads too.

You need to find the appropriate headers for your file type.

You have the <form> action redirect to a page which will service the file. Something like:


<form action="downloadFile.php" method="post">
<input type="submit" name="submit" value="Download File" />
</form>

Then your PHP file would serve your file, for an example we’ll send a PDF file


<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?> 

That would serve the file ‘original.pdf’ for the user to download and prompt them to save it as the filename ‘downloaded.pdf’

Hi Guys,

the way i have my uploads is , when a user uploads a file i append a random 8 digits to the name, so if i upload grahams_file.pdf it’s renamed 12345678.pdf then stored into mysql as the new renamed file.

how would the form know to look for a dynamic name kinda thing? or does it not really matter?

^ hope that makes sense guys

Graham

Graham’

Your form could use a select box, I take it that not only do you store your
numeric alias but the original file name somewhere. Get filename and display using a function as :


function DisplayFiles() {

    $results = GetFiles(); // Function that gets files available for download
    $row = mysql_fetch_assoc( $results ); // Get first row

    echo "    <select name='files'>\
";
    while ( $row ) {
        echo        "<option value = '" . $row['EightDigitValue'] . "'>" .$row['FileName']. "</option>\
";
        $row = mysql_fetch_assoc( $results );
    }
    echo "    </select>\
";
}