Hi it's my very simple example (4 files named as typed in comments): First create a database (images) and table in it: -- -- Database: `images` -- -- Table structure for table `image` -- CREATE TABLE `image` ( `id` int(11) NOT NULL auto_increment, `type` varchar(16) NOT NULL default '', `stream` blob NOT NULL, KEY `id` (`id`) ) TYPE=MyISAM; more create files: [PHP] <!--
upload_image.php--> <form action="insert_image.php" method="post" enctype="multipart/form-data" name="form"> <input type="file" name="file" value="*.*" size="50"> <input type="submit" value="Submit"> </form> [/PHP] [PHP] <!-- insert_image.php--> <? $filename = $_FILES['file']['tmp_name']; if (($handle = fopen($filename, "rb"))) { $stream = fread($handle, filesize($filename)); fclose($handle);
unlink($_FILES['file']['tmp_name']); $type = $_FILES['file']['type']; $dbh = mysql_connect("localhost", "user", "pass"); mysql_select_db("images", $dbh); $qstr = sprintf("INSERT INTO `image` VALUES ('', '%s', '%s')", mysql_real_escape_string($type), mysql_real_escape_string($stream)); mysql_query($qstr, $dbh) or die(mysql_error()); } header("Location: show_image.php"); ?> [/PHP] [PHP] <!-- show_image.php--> <?
$dbh = mysql_connect("localhost", "user", "pass"); mysql_select_db("images", $dbh); $qstr = "SELECT `id` FROM `image`"; $res = mysql_query($qstr, $dbh) or die(mysql_error()); while ($row = mysql_fetch_assoc($res)) { echo 'Image: <img src="image.php?id='.$row["id"].'"><br>'; } ?> <a href="upload_image.php">Upload more...</a> [/PHP] [PHP] <!-- image.php--> <? $dbh = mysql_connect("localhost", "user",
"pass"); mysql_select_db("images", $dbh); $qstr = "SELECT * FROM `image` WHERE `id`=".intval($_REQUEST["id"]); $result = mysql_query($qstr, $dbh); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); $type = $row["type"]; header("Content-type: ".$type); echo $row["stream"]; } ?> [/PHP] P.S. Images no more than 64Kb otherwise field `stream` must be another type. In strings mysql_connect("localhost", "user",
"pass") change "user", "pass" for your value. How can I replace an image file when editing a database record that contains an image? This is my code that displays the image to be edited: </tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap" class="style4"> </td>
<td><img src="<?php echo $row_Member_info['image1']; ?>" width="100%" /><p align="center"><strong>Image 1</strong></td>
</tr>
This is what I am trying to add to replace the image file with another image but does not work: <tr valign="baseline">
<td align="right" nowrap="nowrap" class="style4">Change Image1:</td>
<td colspan="2" class="style1">Choose a file to upload: <input name="image1" type="file" /><br></td>
<td class="style1"> </td>
</tr>
This is my code for updating the record which works except for replacing the image. The new image is not writing to the database and not saving to the 'uploads/' folder. Also, does not generate any error
code: // This displayes the selected row from the previous page
$colname_Member_info = "-1";
if (isset($_GET['recordID'])) {
$colname_Member_info = $_GET['recordID'];
}
mysql_select_db(strato11_members);
$query_Member_info = sprintf("SELECT * FROM Member_info WHERE id = %s", GetSQLValueString($colname_Member_info, "text"));
$Member_info = mysql_query($query_Member_info) or die(mysql_error());
$row_Member_info = mysql_fetch_assoc($Member_info);
$totalRows_Member_info = mysql_num_rows($Member_info);
// Form1 starts here to update the selected record
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
// added by Jim to set image formats
function GetImageExtension($imagetype) {
if(empty($imagetype)) return false;
switch($imagetype) {
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
// added by JK to process image1
$target = "uploads/";
if (isset($_FILES["image1"]["tmp_name"]) && !empty($_FILES["image1"]["tmp_name"])) {
$temp_name=$_FILES["image1"]["tmp_name"];
$imgtype=$_FILES["image1"]["type"];
$ext= GetImageExtension($imgtype);
//This is the directory where images will be saved
if (!$ext) die("Upload file format is not allowed");
$target1 = $target . basename( $_FILES['image1']['name']);
if(move_uploaded_file($temp_name, $target1)) {
} else {
exit("Error While uploading image on the server");
}
}
// JK code ends
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE `Member_info` SET name=%s, rank=%s, departed=%s, service=%, lodd=%s, veteran=%s, branch=%s, decorated=%s, comments=%s, image1=%, cemetery=%s WHERE id=%s",
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['rank'], "text"),
GetSQLValueString($_POST['departed'], "text"),
GetSQLValueString($_POST['service'], "text"),
GetSQLValueString($_POST['lodd'], "text"),
GetSQLValueString($_POST['veteran'], "text"),
GetSQLValueString($_POST['branch'], "text"),
GetSQLValueString($_POST['decorated'], "text"),
GetSQLValueString($_POST['comments'], "text"),
GetSQLValueString($_POST['cemetery'], "text"),
GetSQLValueString($target1,"text"),
GetSQLValueString($_POST['id'], "text"));
// code for entering it into a database.
mysql_select_db(strato11_members);
$Result1 = mysql_query($updateSQL) or die(mysql_error());
$updateGoTo = "view_members.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
|