...

View Full Version : File download help



PRodgers4284
02-15-2008, 10:32 AM
I am having problem with a file download script, i can upload the file fine but i can same to download it, when i select download the page appears blank. Can anyone help:

my code is:


<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include("database.php");

$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");

readfile($filePath);


exit;
}

?>

<html>
<head>
<title>Download CV</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include("database.php");

$query = "SELECT id, name FROM upload2";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download2.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php
}
}
?>
</body>
</html>

rafiki
02-15-2008, 11:42 AM
exit;
i guess this makes the page blank!!

PRodgers4284
02-15-2008, 12:11 PM
exit;
i guess this makes the page blank!!

Thanks for the reply, ive tried removing it but i still cant get the page to display anything, is there nethin else i can try?

rafiki
02-15-2008, 12:14 PM
have you got a link?

PRodgers4284
02-15-2008, 12:24 PM
have you got a link?

I have the file uploading to a folder on the server called "upload" and it is also stored in a database table, the name of file, size, type etc, that works fine. I just cant see anything once i run the download script to download and view the file.

This is the upload code, if it helps any


<head>
<title>Upload CV</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?php
$uploadDir = 'C:/wamp/www/upload/';


if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// the files saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include("database.php");

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>

rafiki
02-15-2008, 01:54 PM
// force to download a file
$file = "http://localhost/test/".$_GET['file']."";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

found this code earlier today whilst browsing around, try it see if it works.

PRodgers4284
02-15-2008, 02:37 PM
// force to download a file
$file = "http://localhost/test/".$_GET['file']."";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

found this code earlier today whilst browsing around, try it see if it works.

Thanks for that rafiki

rafiki
02-15-2008, 03:39 PM
did it work?

PRodgers4284
02-15-2008, 04:41 PM
did it work?

Got it sorted, thanks again for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum