Forum Moderators: open

Message Too Old, No Replies

Partial filename from user input

         

inchman254

8:11 pm on Jan 15, 2009 (gmt 0)

10+ Year Member



Hi. I'm new here and know just the basics of coding html. I've done some forms and often build very basic pages through text editors.

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.

eelixduppy

9:08 pm on Jan 15, 2009 (gmt 0)



Hello and Welcome to WebmasterWorld!

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. :)

inchman254

4:18 am on Jan 27, 2009 (gmt 0)

10+ Year Member



Thanks for that...

I'm getting a glimpse of what I need to do now.

Cheers

inchman254

1:12 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



Hello again,

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;
?>

eelixduppy

9:18 pm on Jan 27, 2009 (gmt 0)



Make sure that you don't have any whites space in your file before the
<?php
tag, and any other white space that could be potentially creating a problem.

inchman254

10:31 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



Jeesh.

That was exactly it.

Like I said, just enough to be dangerous (to myself).

Thanks, man, you saved me another sleepless night.

eelixduppy

7:44 am on Jan 28, 2009 (gmt 0)



If it's not one thing it's another. Glad you got it resolved. :)