February 1, 201214 yr Hi everyone, I'm trying desperately to have this form operate the way I want it to. Basically, when the user submits this form, I want a download box to open for the PDF target file, rather than the PDF loading in the browser. Here is the code: <FORM METHOD="POST" ACTION="../cgi-bin/sender.pl" name="Free Report"> <input type=hidden name="redirect" value="../Report.pdf" /> <input type="submit" id="button" value="Download Now!" onclick="MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue" /> Any help would be much appreciated, thank you! Edited February 2, 201214 yr by Renaissance-Design Please use the code button or tags to format your code.
February 1, 201214 yr OK, you don't want the form to be linking directly to the PDF as the browsers default behavior (to load it in the browser) will take precedence. I assume thats whats happening (the pdf name field is redirect so I suspect your script redirects to the file?) Instead you want the form to lead to a script that will cause the file (the PDF) to download, basically by setting a header type and loading the file that way. I'll give you some code but it is untested and is just about as simple as it gets (but there again it doesn't need to be particularly complex. So your form tag will look something like this: <form action="download.php" method="post"> Then you will have a PHP script to download the file in question: <?php // Set the details of the file to load, this may be hard coded or dynamic (i.e. form has a fileid field // that this script can use to lookup the file in the database. $file['filename'] = "somefile.pdf"; // The path to the file $filepath = "files/" . $file['filename']; // Set the content-type header('Content-type: text/plain'); // This will be the type for the file, so you'll want to change this // Will cause the script to show the Open / Save dialog header("Content-Disposition: attachment; filename=\"$filepath\""); // Read the file from the server readfile("$filepath"); ?> Or you can do it like this: <?php // ------------------------------------------------------------------------------------------------------- // Do file download // ------------------------------------------------------------------------------------------------------- $fileid = $_POST['fileid']; // Clean your input here using whatever mechanism you are using // Validate those details and fetch the files details from the DB here, or just set them // The files details will go into $download /* ... The $download array should contain at a minimum $download['filename'] // The name of the file (i.e. 'myPDFFile', no dot or extension) $download['format'] // The format of the file (pdf, no '.') $download['mimetype'] // The mimetype of the file (PDF is 'application/pdf') ... */ // OK, set the files download path (tidy this up) $filepath = "files/"; $fullpath = $filepath . $download['filename'] . "." . $download['format']; $filename = $download['filename'] . "." . $download['format']; // Fetch the file if ($fd = fopen ($fullpath, "r")) { $fsize = filesize($fullpath); // Get the filesize $ext = $download['format']; // Set the format into $ext switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for different extensions header("Content-Disposition: attachment; filename=\"".$filename."\""); // use 'attachment' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$filename."\""); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; ?> I notice your script, in the form header, is a perl script. I haven't used perl since Ultimate Bulletin Board was king of the communities (I used to love hacking that forum, shame its gone now... ah the nostalgia)... Shame they screwed it all up. anyway... I cannot remember the code for causing a file to download in perl, but if I remember correctly its something pretty similar to the second example. The first I just showed for demonstrative purposes, use the second to power it if you use PHP. Otherwise google "file download in perl" and it should come up with the answer. Edited February 1, 201214 yr by FizixRichard
Create an account or sign in to comment