Forum Moderators: open
IOW, dangerous!
I had a look around the site and wasn't able to find an answer to this.
I want to be able to allow the user to enter their id and that ID makes up part of a file name, then have the page download the file for them through the "Save As" dialogue.
For example... input box, user enters 12345
System starts download of file named abc12345.pdf
Is there an easy way to do this?
TIA.
If you are going to be serving this file with some kind of server-side scripting language, such as PHP, all you would have to do would be to correctly change the headers (for the download to occur as you want), but also add the additional text to the beginning of the filename so that it is the file that you want. It would be a simple string concatenation. :)
I was able to put together a download script using your suggestion and some code that was posted on a forum. The problem that I'm having is that the script is causing an initial carriage return in the downloaded file. It is an iCal file and the ingesting application doesn't like a blank line to start.
Any suggestions would be more than welcome.
I attempted to read and discard the first byte before the first echo, thinking that it might be reading a <CR> as part of the first fread(), but it would appear that the echo itself is causing the initial carriage return.
Here is the code I am using.
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/pbsplus/calendars/"; // play with the path if the document root does noet exist
$month = $_REQUEST["month"];
$empno = $_REQUEST["empno"];
$dlfile = strtolower($month).$empno.".ics" ;
$fullPath = $path.$dlfile;
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachement' to force a download
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;
?>
<?php tag, and any other white space that could be potentially creating a problem.