Forum Moderators: coopster
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>
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>