I'm trying to upload two images into a mysql database and then display one of them. It seems to upload fine - the info is written to the database, at least - but when I try to view the image, all that shows up is a full page of weird characters like this: ��J
Can anyone tell me where I've gone wrong? I've read everything I can and nothing I try works.
Here's the upload script (add_art.php):
<?php
if(isset($_POST['added']) && $_FILES['w_fileS']['size'] > 0 && $_FILES['w_fileT']['size'] > 0) {
$w_title=$_POST['w_title'];
$w_medium=$_POST['w_medium'];
$w_year=$_POST['w_year'];
$w_description=$_POST['w_description'];
$filenameS = $_FILES['w_fileS']['name'];
$tmpNameS = $_FILES['w_fileS']['tmp_name'];
$filesizeS = $_FILES['w_fileS']['size'];
$filetypeS = $_FILES['w_fileS']['type'];
$filenameT = $_FILES['w_fileT']['name'];
$tmpNameT = $_FILES['w_fileT']['tmp_name'];
$filesizeT = $_FILES['w_fileT']['size'];
$filetypeT = $_FILES['w_fileT']['type'];
$fpS = fopen($tmpNameS, 'r');
$filedataS = fread($fpS, filesize($tmpNameS));
$filedataS = addslashes($filedataS);
fclose($fpS);
$fpT = fopen($tmpNameT, 'r');
$filedataT = fread($fpT, filesize($tmpNameT));
$filedataT = addslashes($filedataT);
fclose($fpT);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database because:".mysql_error());
$add_all="INSERT INTO agni_work VALUES (
'',
'$w_title',
'$w_medium',
'$w_year',
'$w_description',
'$filenameS',
'$filesizeS',
'$filetypeS',
'$filedataS',
'$filenameT',
'$filesizeT',
'$filetypeT',
'$filedataT'
)";
mysql_query($add_all) or die("Unable to insert new data into database because:".mysql_error());
include 'library/closedb.php';
mysql_close();
echo("POST SUBMITTED SUCCESSFULLY");
}
else {
?>
<h1>Add a New Piece</h1>
<form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>">
<p>Title of Piece:<br />
<input type="text" name="w_title" size="40" maxlength="50" />
</p>
<p>Medium/Materials:<br />
<input type="text" name="w_medium" size="40" maxlength="50" />
</p>
<p>Year:<br />
<input type="text" name="w_year" size="20" maxlength="4" />
</p>
<p>Piece Description:<br />
<textarea name="w_description" rows="8" cols="40">
</textarea></p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<p>Standard Image File:<br />
<input name="w_fileS" type="file" id="w_fileS" /></p>
<p>Thumbnail Image File:<br />
<input name="w_fileT" type="file" id="w_fileT" /></p>
<p><input type="submit" value="Add Piece"/></p>
<input type='hidden' name='added' value='set' />
</form>
<?php
}
?>
Here's the script that gets the image from the database (do_getfileS.php):
<?php
include("dbinfo.inc.php");
$w_id = $_GET['w_id'];
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database because:".mysql_error());
$query="SELECT filenameS, filesizeS, filetypeS, filedataS FROM agni_work WHERE w_id= $w_id";
$result=mysql_query($query) or die("Unable to get image from database because:".mysql_error());
$data = @MYSQL_RESULT($result,0,"filedataS");
$type = @MYSQL_RESULT($result,0,"filetypeS");
$name = @MYSQL_RESULT($result,0,"filenameS");
$size = @MYSQL_RESULT($result,0,"filesizeS");
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $data;
?>
From what I've read, I should be able to use <img src="do_getfileS.php?w_id=1" /> to view the image with w_id equal to 1, but it doesn't work.
I appreciate any help you can give. Thanks!