PDA

View Full Version : PHP Help Please



A G E N T
08-31-2007, 11:24 PM
Basically I was trying to make a download script where you put in the name of the file you wanted into a box, it looks for that file in one directory (always the same one, called /downloads/). I can't seem to get it to work though, it never finds the file properly. If anyone knows what they're doing, can they please help me? Thanks :)



<html>
<div align='center'><font size=20>DOWNLOADS</font></div>
<br>
Files available for download:<br>
<ul>
<li>testfile.txt
</ul>

<?php

echo "<form method='post' action='download.php'>
<input name='file' type='text'>
<input type='submit'>
</form>";

$file=$_REQUEST['file'];
if (!isset($file)) {die("Please specify file name for download.");}

function find_file($fname)
{

if (is_file('/downloads/'.$fname))
{
$file_path = '/downloads/'.$fname ;
return($file_path);
}
}

$fsize = filesize($file_path);

if (find_file($file))
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file_path");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
}else{
echo "That file does not exist. Please double-check your spelling.";
}
?>

</html>

Wilio
09-02-2007, 12:30 AM
May I suggest to keep the least Html into PHP.
For example here:

echo "<form method='post' action='download.php'>
<input name='file' type='text'>
<input type='submit'>
</form>"; It's just messy. keep it out of your php.

Just to point it out.
And what is the problem?(what the debugger says?)

A G E N T
09-02-2007, 04:10 AM
I know it's messy, but I'm just not sure how to make PHP forms. :o

The script executes properly, so it's not an error, per se, just the script isn't working as I expected it to. The script doesn't find the files properly...I think there is something wrong with find_file.