Trouble with downloading an image from Mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brancook
Forum Newbie
Posts: 6
Joined: Sat Oct 20, 2007 8:34 pm

Trouble with downloading an image from Mysql database

Post by brancook »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I ran into an issue with one of my applications. When I click a link to a file that is in a Mysql database I get the following error: File with given ID not found in database! Which is actually an error message that I have setup. I've stared at the code for two straight days and i can't find the problem. Here is my code if anyone can help. I've placed some comments within my code to point where I think the issues are happening:

Code: Select all

<div id='column2'>
			<h1>View the Status of an RMA</h1>
			<br />
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
				method="POST" enctype="multipart/form-data">
<?php
//drop down menu selects everything from rma table 
echo'<select name="rma">';
echo '<option value="none">Select RMA to View Status&nbsp;&nbsp;&nbsp;</option>'."\n";
$res=mysql_query("select * from rma");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option>$row[rma_number]</option>";
}
echo'</select>';
?>
<br />
				<input type="submit" value="Submit" />
			</form>

<?php
if (isset($_GET['action'])) {
	$action = ($_GET['action']);
} else {
	$action = '';
}		
//something with the $id just isn't adding up. 
if (($action == 'view' or $action == 'dnld') and isset($_GET['id'])) {
	$id = ($_GET['id']);

//User is retrieving the file
$sql = "SELECT filename, mimetype, filedata FROM dropbox WHERE id = '$id'";
$result = mysql_query($sql);
if (!$result) {
	exit('Database error: ' . mysql_error());
}

$file = mysql_fetch_array($result);
if (!$file) {
	exit('File with given ID not found in database!');
}
$filename = $file['filename'];
$mimetype = $file['mimetype'];
$filedata = $file['filedata'];
$dispostion = 'inline';

if ($action == 'dnld') {
	$dispostion = 'attachment';
	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
	$mimetype = 'application/x-download';
	}
}

header("content-dispostion: $dispostion; filename=$filename");
header("content-type: $mimetype");
header('content-length: ' . strlen($filedata));

echo $filedata;
exit();
}
//$targetrma in sql statement will display result from submitted record from drop down menu
$targetrma = ($_POST['rma']);
$sql = "SELECT rma.rma_number, status.status_name, ref_categories.category_name, filename, date, description 
FROM dropbox, rma, status, ref_categories 
WHERE dropbox.ref_status_id=status.id AND dropbox.ref_categories_id=ref_categories.id AND dropbox.ref_rma_id=rma.id AND rma.rma_number = '$targetrma'
ORDER BY date";
$filelist = @mysql_query($sql);
if (!$filelist) {
exit('Database error: ' . mysql_error());
} 
?>
<br />
<br />
<h1><?php echo $_POST['rma']; ?></h1>
<br />          
<p>The following files are stored in the database:</p>

<div align="center">
<table id="rmaview">
<tr>
	<th>RMA Number</th>
	<th>RMA Status</th>
	<th>Filename</th>
	<th>Date</th>
	<th>Note</th>
	<th>Unit Type</th>
	<th>Download File</th>
</tr>
<?php
if (mysql_num_rows($filelist) > 0) {
	while ($f = mysql_fetch_array($filelist)) {
	?>
	<tr>
		<td>
			<?php echo $f['rma_number']; ?>
		</td>
		<td>
			<?php echo $f['status_name']; ?>
		</td>
		<td>
			<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=dnld&id=<?php echo $f['id']; ?>"><?php echo $f['filename']; ?></a>
		</td>
		<td>
			<?php echo $f['date']; ?>
		</td>
		<td>
			<?php echo  $f['description']; ?>
		</td>
		<td>
			<?php echo  $f['category_name']; ?>
		</td>
		<td>
			<!--<a href="<?php //echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php //echo $f['id']; ?>"><?php //echo $f['filename']; ?></a>-->
		</td>
</tr>
	
	<?php
	}
} else {
	?>
	<tr><td>No Files!</td></tr>
	<?php
}
?>
</table>	
</div>
Thanks in advance for any advice.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
brancook
Forum Newbie
Posts: 6
Joined: Sat Oct 20, 2007 8:34 pm

comments

Post by brancook »

Sorry I have some comments where they shouldn't be. The last <td> in the table actually looks like this:

<td>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>"><?php echo $f['filename']; ?></a>
</td>
brancook
Forum Newbie
Posts: 6
Joined: Sat Oct 20, 2007 8:34 pm

got it

Post by brancook »

I had to call out the table and id for the image in my sql statement. I also had to move all of my code above my xhtml because once I solved the image problem was getting header errors.
Post Reply