Hi all, I have the following script as a file uploader, but get the error in the title (‘Duplicate entry ‘0’ for key 1’) when trying to upload a second file although the first uploads fine.
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ob_start();
// Quickly grab the config file
include('config.php');
if (isset($_GET['action']))
{
$action = $_GET['action'];
}
else
{
$action = '';
}
if (($action == 'view' || $action == 'dnld') && isset($_GET['id']))
{
$id = (int) $_GET['id'];
// User is retrieving a file
$sql = "SELECT filename
, mimetype
, filedata
FROM whag
WHERE id = $id";
$result = mysql_query($sql) or die(mysql_error());
if (! $result)
{
exit('Database error: ' . mysql_error());
}
$file = mysql_fetch_array($result);
print_r($file);
if (! $file)
{
exit('File with given ID not found in database!');
}
$filename = $file['filename'];
$mimetype = $file['mimetype'];
$filedata = $file['filedata'];
$disposition = 'inline';
if ($action == 'dnld')
{
$disposition = 'attachment';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7'))
{
$mimetype = 'application/x-download';
}
}
ob_end_clean();
header('Content-disposition: $disposition; filename=' . $filename);
header('Content-type: $mimetype');
header('Content-length: ' . strlen($filedata));
echo $filedata;
exit;
}
elseif($action == 'del' && isset($_GET['id']))
{
$id = (int) $_GET['id'];
// User is deleteing a file
$sql = "DELETE FROM whag WHERE id = $id";
$ok = mysql_query($sql) or die(mysql_error());
if (! $ok)
{
exit('Database error: ' . mysql_error());
}
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
elseif(isset($_FILES['upload']))
{
// Bail out if the file isn't really an upload.
if (! is_uploaded_file($_FILES['upload']['tmp_name']))
{
exit('There was no file uploaded!');
}
$uploadfile = $_FILES['upload']['tmp_name'];
$uploadname = $_FILES['upload']['name'];
$uploadtype = $_FILES['upload']['type'];
$uploaddesc = $_POST['desc'];
$date = date('Y-m-d');
// Open file for binary reading ('rb')
$tempfile = fopen($uploadfile, 'rb');
// Read the entire file into memory using PHP's filesize function to get the file size.
$filedata = fread($tempfile, filesize($uploadfile));
// Prepare for database insert by adding backslashes before special characters.
$filedata = addslashes($filedata);
// Create the SQL query
$sql = "INSERT INTO whag
SET filename = '$uploadname'
, mimetype = '$uploadtype'
, description = '$uploaddesc'
, filedata = '$filedata'
, date = '$date' ";
// Perform the insert
$ok = mysql_query($sql) or die(mysql_error());
if (! $ok)
{
exit('Database error storing file: ' . mysql_error());
}
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
// Default page view: lists stored files
$sql = 'SELECT id
, filename
, mimetype
, description
FROM whag ORDER BY date DESC';
$filelist = mysql_query($sql) or die(mysql_error());
if (! $filelist)
{
exit('Database error: ' . mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $siteName; ?> - PHP/MySQL File Respositery</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="admin.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrap">
<h1><?php echo $siteName; ?></h1>
<form action"<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p><label>Upload File<br />
<input type="file" name="upload" /></label></p>
<p><label>Date<br />
<input type="text" name="desc" maxlength="225" /></label></p>
<p><input type="submit" value="Upload" /></p>
</form>
<p>The following files are stored in the database:</p>
<table cellspacing="0">
<tr>
<th class="alt">Filename</th>
<th>Date</th>
<th class="alt">Type</th>
<th class="end" colspan="2">Download/Delete</th>
</tr>
<?php
if (mysql_num_rows($filelist) > 0)
{
while ($f = mysql_fetch_array($filelist))
{
?>
<tr valign="top">
<td class="alt"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>"><?php echo $f['filename']; ?></a></td>
<td><?php echo $f['description']; ?></td>
<td class="alt"><?php echo $f['mimetype']; ?></td>
<td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=dnld&id=<?php echo
$f['id']; ?>">Download</a></td>
<td class="end"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=del&id=<?php echo $f['id']; ?>" onclick="return confirm('Delete the file?');">Delete</a></td>
</tr>
<?php
}
}
else
{
?>
<tr><td colspan="3">No Files!</td></tr>
<?php
}
?>
</table>
</div><!-- #wrap -->
</body>
</html>