Hi,
What I want to do is set up a script so the user can upload an image on their computer the mysql database using a form. And then using a hyperlink on the same page download it to the page in their web site.
The script uploads fine creates the hyperlink to the download page but does not download any thing. The download page comes up but nothing is downloaded to the page.
I have created 2 pages. This is the script for imageform.php: this is where the form is and the script to upload and the script that creates the hyperlink to download the file:
<?php include("connect.php");
if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$image = fread($fp, filesize($tmpName));
$image = addslashes($image);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query1 = "INSERT INTO images (name, size, type, image ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$image')";
$result = mysqli_query($connection, $query1);
echo "<br>File $fileName uploaded<br>";
$query = "SELECT id, name FROM images";
$result = mysqli_query($connection, $query) or die('Error, query failed');
if(mysqli_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysqli_fetch_array($result))
{
?>
<a href="downloadimage.php?id=<?php echo $id ;?>"><?php echo $name;?></a></br>
<?php
}
}
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#Layer1 {
position:absolute;
width:200px;
height:115px;
z-index:1;
}
-->
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<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" id="userfile">
</td></tr>
<tr>
<td style="padding-top:10px;" width="80"><input name="submit" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
And downloadimage.php: this has the script for downloading the image and is the page where I wish the picture to be downloaded to:
<?php include("connect.php");
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$id = $_GET['id'];
$query = "SELECT name, type, size, image FROM images WHERE id = '$id'";
$result = mysqli_query($connection, $query) or die('Error, query failed');
list($name, $type, $size, $image) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $image;
exit;
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div></div>
</body>
</html>
Please help, its probably sommething really oibvious but its sending me bananas!
thanks
cass27๐