Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Storing Images in MySQL with PHP

php mysql

  • This topic is locked This topic is locked
1 reply to this topic

#1 Tongleang

Tongleang

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C
  • Learning:C

Posted 18 October 2012 - 01:00 AM

Hi! I have 5 file like below:

*****create database and table****

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// Create database
if (mysql_query("CREATE DATABASE my_dbimg",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

// Create table
mysql_select_db("my_dbimg", $con);
$sql = "CREATE TABLE img_tbl
(
id int(4) NOT NULL AUTO_INCREMENT,
img_name varchar(255) collate latin1_general_ci NOT NULL,
img_type varchar(4) collate latin1_general_ci NOT NULL,
img_size int(8) NOT NULL,
img_data blob NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;";
// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>
***create form upload***
FORM NAME="f1" METHOD="POST" ACTION="upload.php" ENCTYPE="multipart/form-data">
<table>
<tr><td> Image Upload Page </td></tr>
<tr><td> <input type="file" name="imgfile"/></td></tr>
<tr><td> <input type="submit" name="submit" value="Save"/> </td></tr>
</table>
</FORM>
****file dbconfig.php***
<?
$dbhost = "localhost"; // server host name
$dbusr = "root"; // mysql username to connect
$dbpass = ""; //password
$dbname = "my_dbimg"; //database name to select to
?>
****file upload.php***
<?
include "dbconfig.php";

$dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die('Could not connect: ' . mysql_error());

mysql_select_db($dbname, $dbconn);

if(isset($_REQUEST['submit']) && $_FILES['imgfile']['size'] > 0)
{
$fileName=$_FILES['imgfile']['name']; // image file name
$tmpName=$_FILES['imgfile']['tmp_name']; // name of the temporary stored file name
$fileSize=$_FILES['imgfile']['size']; // size of the uploaded file
$fileType=$_FILES['imgfile']['type']; // file type

// Read the file
$fp = fopen($tmpName, 'r');
$imgContent = fread($fp, filesize($tmpName));
$imgContent = addslashes($data);
$imgContent1=base64_encode($imgContent);
fclose($fp); // close the file handle

$query = "INSERT INTO img_tbl ('img_name','img_type','img_size','img_data') VALUES ('$fileName','$fileType', '$fileSize','$imgContent1')";

mysql_query($query) or die('Error, query failed');
$imgid = mysql_insert_id(); // autoincrement id of the uploaded entry
mysql_close($dbconn);

echo "<br>Image successfully uploaded to database<br>";
echo "<a href=\"viewimage.php?id=$imgid\">View Image</a>";

}else die("You have not selected any image");
?>
*****file viewimage.php***
<?
if(isset($_REQUEST['id']))
{
// get the file with the id from database
include "dbconfig.php";
$dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die("Error Occurred-".mysql_error());
mysql_select_db($dbname, $dbconn) or die("Unable to select database");

$id= $_REQUEST ['id'];
$query = "SELECT 'img_name','img_type','img_size','img_data' FROM img_tbl WHERE id = '$id'";

$result = mysql_query($query) or die(mysql_error());
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
print base64_decode($content);
mysql_close($dbconn);
}
?>
***file showimages.php****
<?
include "dbconfig.php";
$dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die("Error Occurred-".mysql_error());
mysql_select_db($dbname, $dbconn) or die("Unable to select database");
$query = "SELECT 'id','img_name','img_type','img_size','img_data' FROM img_tbl ORDER BY 'id'";
$result = mysql_query($query) or die('Error, query failed');
while($row = mysql_fetch_array($result)){
echo "<img src=\"viewimage.php?id=$row[id]\" width=\"55\" height=\"55\"/> <br/>";
}
mysql_close($dbconn);
?>

When I run tbluploadimg.html it show message like below:

0) { $fileName=$_FILES['imgfile']['name']; // image file name $tmpName=$_FILES['imgfile']['tmp_name']; // name of the temporary stored file name $fileSize=$_FILES['imgfile']['size']; // size of the uploaded file $fileType=$_FILES['imgfile']['type']; // file type // Read the file $fp = fopen($tmpName, 'r'); $imgContent = fread($fp, filesize($tmpName)); $imgContent = addslashes($data); $imgContent1=base64_encode($imgContent); fclose($fp); // close the file handle $query = "INSERT INTO img_tbl ('img_name','img_type','img_size','img_data') VALUES ('$fileName','$fileType', '$fileSize','$imgContent1')"; mysql_query($query) or die('Error, query failed'); $imgid = mysql_insert_id(); // autoincrement id of the uploaded entry mysql_close($dbconn); echo "
Image successfully uploaded to database
"; echo "View Image"; }else die("You have not selected any image"); ?>

How to resolve it. Help me.
  • 0

#2 BenW

BenW

    CC Devotee

  • Community Manager
  • 976 posts
  • Programming Language:ActionScript
  • Learning:ActionScript

Posted 18 October 2012 - 01:11 AM

You appear to have two other threads already open on this topic. Please pick one of them and update the topic as you progress rather than making new threads. I'm closing this thread because there are already people trying to help in one of your other topics, and it makes it easier for everyone if the conversation is kept in one place. It's also polite to acknowledge the help offered to this point :)
  • 0

fss_overall.png

 






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download