Download system virker ikke rigtig
HejJeg har et problem med et upload script og et download script.
Jeg har en apache webserver kørerne med PHP og MySql. Jeg kan godt få upload script til at virke og ligge filoplysningerne i databasen men når jeg skal til at downloade filen fra databasen kommer den med de forkerte oplyser med hvor meget filen fylder.
Problemmet kommer kun når jeg laver filnavnet om til et unikt navn.
Opret.php Filen (Upload filen)
<?php require_once('../Connections/database.php'); ?>
<html><strong></strong>
<head>
<title></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>
<strong></strong></head>
<body>
<?
// upload directory
$uploadDir = "C:/filer/";
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = explode(".",$fileName);
$ext = strtolower($ext[1]);
// generate the random file name
$randname = rand(0000,9999);
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randname . '.' . $ext;
echo "Filename: ".$fileName." Extension: ".$ext." Filepath: ".$filePath."<br>";
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO filer (name, newname, size, type, date, description) ".
"VALUES ('$fileName', '$randname.$ext', '$fileSize', '$fileType', '$date', '$description')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br><b>The file was successfuly uploaded.</b><br><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>
Download.php
<?php
require_once('database.php');
error_reporting(E_ALL);
if(isset($_GET['id']))
{
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM filer WHERE id = '$id'";
$result = mysql_query($query) or die(mysql_error());
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);
}
?>