PDA

View Full Version : Help with PHP, please!



A G E N T
09-02-2007, 04:41 AM
Hi there, this was originally in "other programming languages", until I realized that there was a perfectly good web languages forum. Anyway, here is the post:

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>

Anyone who can help me get this to work would be greatly appreciated. Also, if anyone knows how to work mySQL, please PM me. It would be appreciated too... but more so the PHP ;)

A G E N T
09-03-2007, 01:46 AM
Ah, I see...thanks! :) Figures it would be something like that, eh? Well...thanks again!

LordGregGreg
09-03-2007, 03:04 AM
wow, good eye!

code, you ever been a progrmaing teacher b4?

A G E N T
09-08-2007, 08:03 PM
Having a new problem with this script, not sure what I'm doing wrong though...
Basically it sends the file, but it includes some code (HTML code) in the file. Can anyone help?


<HTML>
<title>Download Page</title>

<form name="DL" method="POST">
<input name="fname" type="text"><input name="Submit" type="submit">
</form>

<?php

$extensions = array (

'zip' => 'application/zip',


'pdf' => 'application/pdf',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'txt' => 'text/standard',


'exe' => 'application/octet-stream',


'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',


'mp3' => 'audio/mpeg',
'wav' => 'audio/x-wav',


'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo'
);


$file = $_POST['fname'];

if(eregi("/",$file)) { exit("Possible directory traversal attack detected");}

$fext = strtolower(substr(strrchr($file,"."),1));
$file_path = ('downloads/'.$file);

if (function_exists('mime_content_type'))
{ $mtype = mime_content_type($file_path); }

else { if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME); // return mime type
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);

} else { $mtype = $extensions[$fext]; }}


if (isset($_POST['Submit']))
{
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=$file");
readfile("downloads/".$file);
}

?>
</HTML>

Bam Bam
09-08-2007, 11:42 PM
My guess is because you are outputting the html before sending the file and it becomes part of the download?

Edit:

Download request form:

<HTML>
<title>Download Page</title>
<form action="downloads.php" name="DL" method="POST" />
<input name="fname" type="text" />
<input name="Submit" type="submit" />
</form>
</HTML>

Download Script (You will see that the form above refers to this as downloads.php):

<?php
$extensions = array(

'zip' => 'application/zip',


'pdf' => 'application/pdf',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'txt' => 'text/standard',


'exe' => 'application/octet-stream',


'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',


'mp3' => 'audio/mpeg',
'wav' => 'audio/x-wav',


'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo'
);


$file = $_POST['fname'];

if(eregi("/",$file)){
die("Possible directory traversal attack detected");
}

$fext = strtolower(substr(strrchr($file,"."),1));
$file_path = ('downloads/'.$file);

if(function_exists('mime_content_type')){
$mtype = mime_content_type($file_path);
}

else{
if(function_exists('finfo_file')){
$finfo = finfo_open(FILEINFO_MIME); // return mime type
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);
}else{
$mtype = $extensions[$fext];
}
}


if (isset($_POST['Submit'])){
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=$file");
readfile("downloads/".$file);
}
?>

Edit 2:
You really should read a little on PHP code standardization. Your standards aren't too standard.

A G E N T
09-09-2007, 02:40 AM
Thanks for the advice, separating it into two pages worked out.
As for standards, I guess I should learn them for when I'm posting my stuff, but other than that, nobody really sees your code but you.

Thanks again!

Dankness
09-11-2007, 02:05 AM
Thanks for the advice, separating it into two pages worked out.
As for standards, I guess I should learn them for when I'm posting my stuff, but other than that, nobody really sees your code but you.

Thanks again!

I used to think that too, until i realized if i cant read my code due to crappy standards then my scripts will always have bugs. When tryin to find the problem in any lang you should always go thru and standardize it first as going thru it like that will gve you a second chance to spot your errors.

lordsaturn
09-11-2007, 04:50 AM
You could always use a beautifier (Auto-standardizer) that comes with some PHP IDEs. But then again it's good to know the standardization so you don't rely on the beautifier 100% of the time.

Dankness
09-16-2007, 01:34 AM
You could always use a beautifier (Auto-standardizer) that comes with some PHP IDEs. But then again it's good to know the standardization so you don't rely on the beautifier 100% of the time.

that totally defeats the purpose of standards. If you can't code properly you'll never be a good coder as bugs love a mess. Go into a messy house and you normally find bugs. go into a clean house and you normally wont . The same is with your applications. Messy app's are normally buggy since the coder didn't take the time to inspect his work throughly.

A G E N T
09-17-2007, 02:13 AM
I guess I should, I've been trying lately to keep things neat if not official, per se. Dankness, you were right 100%, it's a lot easier to find and eliminate errors when it's all neat.

@lordsaturn, I don't use an IDE, I create and edit files right on my FTP control, it's basically an empty text field :mad: . It works for me, though.

Dankness
09-19-2007, 04:39 AM
I guess I should, I've been trying lately to keep things neat if not official, per se. Dankness, you were right 100%, it's a lot easier to find and eliminate errors when it's all neat.

@lordsaturn, I don't use an IDE, I create and edit files right on my FTP control, it's basically an empty text field :mad: . It works for me, though.

You should get Komodo Edit, its the best IDE you'll ever use... has FTP built in i write everything with it. while your at it google for the Charcoal Rainbow Scheme to go with it its awesome.

Heres a screenshot of it.http://http://www.flickr.com/photos/10565188@N03/1195573957/ (http://http//www.flickr.com/photos/10565188@N03/1195573957/)


It has FTP,, SVN,Support for every lang out there,Auto Google Search(Highlight word and hit Ctrl-F1) Search in Directorys/SubDirectorys and much more. I use Komodo IDE but Komodo Edit is free. O and Its multiplatform so you can use it in Linux,Mac or Winblows too.

Dankness
09-20-2007, 12:34 AM
Editplus2[win] or jEdit[win/linux] for me ;-)

have nothing on Komodo, iv used both :P lol c0de why use a free editor when i gave you a licensed 300 editor that owns.