Forum Moderators: coopster

Message Too Old, No Replies

Need a tad bit of help getting images from DB..

         

kelric4

12:30 am on Aug 3, 2004 (gmt 0)

10+ Year Member



Hello there, before anything I want to say that I know storing images in a BLOB field in a DB is not a good idea, but I am trying it out of curiosity.
I have no problems uploading the images into the BLOB, or displaying info about the image once its there, like its name. My problem occurs for some reason when I click the Download link that is supposed to download the image. It trys to make the target php file an image...

here is my code for the page that shows the info about the images in the MySQL table


<?php
require_once ('./mysql_connect.php');

$sql = "SELECT * FROM coh ORDER BY id DESC";
$result = mysql_query($sql);
$rows = mysql_num_rows($result);

echo '<table align="center" width="100%" cellspacing ="2" cellpadding="2">
<tr>
<td align="left"><b>Name</b></td>
<td align="left"> </td>
<td align="left"><b>Date/Time</b></td>
</tr>';

for ($i = 0; $i < $rows; $i++) {
$data = mysql_fetch_object($result);
echo "<tr>
<td align=\"left\">$data->filename</td>
<td align=\"left\">( <a href='uploaded.php?id=$data->id'>Download</a> )</td>
<td align=\"left\">$data->date</td>
</tr>";
}
mysql_free_result($result);
mysql_close();
?>

And here is the code for the php file that is supposed to download the image.

<?php
require_once ('./mysql_connect.php');
$sql = "SELECT name, filename FROM coh WHERE id=$id";

$result = @mysql_query($sql);
$data = @mysql_result($result, 0, "name");
$name = @mysql_result($result, 0, "filename");
$type = 'image/jpeg';

header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
?>

As you can probably notice, I am a rookie at php/mysql. So I would very much apreciate any assistance.

If it helps at all here is the code I used to make the form that enters the pictures into the DB.

<?php
if (isset($_POST['submit'])) {
$message = NULL;
if(empty ($_POST['picture'])) {
$content = FALSE;
$message .= '<p>You forgot to enter your picture\'s URL!</p>';
} else {
$fp = fopen($_POST['picture'], "rb");
$content = fread($fp, filesize($_POST['picture']));
$content = addslashes($content);
}
if(empty ($_POST['filename'])) {
$n = FALSE;
$message .= '<p>You forgot to enter your picture\'s name!</p>';
} else {
$n = $_POST['filename'];
}

if ($content && $n) {
require_once ('./mysql_connect.php');
$query = "INSERT INTO coh (name, date, filename) VALUES ('$content', NOW() , '$n')";
$result = @mysql_query ($query);
$id = mysql_insert_id();
if ($result) {
echo '<font color="blue"><p><b>Your picture has been uploaded.</b></p></font>';
}
mysql_close();
} else {
$message .= '<p>Please try again.</p>';
}
}

if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
</p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<legend>Enter your picture's URL below:</legend>

<p><b>URL:</b> <input type="file" name="picture" size="50" maxlength="200"
value="<?php if (isset($_POST['picture'])) echo $_POST['picture'];?>" />
</p>

<p><b>Enter desired name:</b> <input type="text" name="filename" size="50" maxlength="50"
value="<?php if (isset($_POST['filename'])) echo $_POST['filename'];?>" />
</p>

<div><input type="submit" name="submit" value="Upload" /></div>
</form>

crypto

10:11 am on Aug 3, 2004 (gmt 0)

10+ Year Member



Yes the target php file would be an image because that is what you are making the Content header of the page.

"Content-Type: image/jpeg" will make the page an image.

Try this instead:
"Content-Type: application/octetstream"

Haven't tried it myself so let me know it it works ;)

kelric4

6:11 am on Aug 4, 2004 (gmt 0)

10+ Year Member



Hmm I am not really sure what that did lol.. I want it to actually have an image appear though, the problem is that it isnt getting the image that is set to the id in the DB; and I can't seem to understand why

crypto

7:57 am on Aug 4, 2004 (gmt 0)

10+ Year Member



I just noticed that the <form> that you are using to upload the image file doesn't have the attribute "enctype".

Modify the following tag:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

To This:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">

This is necessary whenever you are uploading files through the <form>