codeoxygen
asked on
display images in php
facing problem in displaying the images that are stored in databases the following code is to display the images
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('upload', $link);
if (!$db_selected) {
die ('Database error : ' . mysql_error());
}
$sql = "SELECT * FROM images";
header("Content-type: image/jpeg");
echo mysql_result($result, 0,'imageData');
?>
the following code is to upload the images in the database
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('upload', $link);
if (!$db_selected) {
die ('Database error : ' . mysql_error());
}
$maxFileSize = "1000000";
$image_array = array("image/jpeg","image/ jpg","imag e/gif","im age/bmp"," image/pjpe g","image/ png");
$fileType = $_FILES['userfile']['type' ];
$msg = '';
if(@$_POST['Submit'])
{
if (in_array($fileType, $image_array))
{
if(is_uploaded_file($_FILE S['userfil e']['tmp_n ame']))
{
if($_FILES['userfile']['si ze'] < $maxFileSize)
{
$imageData =addslashes (file_get_contents($_FILES ['userfile ']['tmp_na me']));
$sql = "INSERT INTO images ( imageData) VALUES ('$imageData')";
mysql_query($sql) or die(mysql_error());
$msg = " Data successfully uploaded";
}
else
{
$msg = ' Error : File size exceeds the maximum limit ';
}
}
}
else
{
$msg = 'Error: Not a valid image ';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<span style="color:#FF0000"><?ph p echo $msg; ?></span><br />
<h3>Select File to upload</h3>
<form enctype="multipart/form-da ta" action="" method="post">
<input name="userfile" type="file" />
<input type="submit" value="Submit" name="Submit"/>
</form>
</body>
</html>
i am able to upload the images here in the database but i am unable to display all the images that are stored in the database.help me out experts
<?php
$link = mysql_connect('localhost',
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('upload', $link);
if (!$db_selected) {
die ('Database error : ' . mysql_error());
}
$sql = "SELECT * FROM images";
header("Content-type: image/jpeg");
echo mysql_result($result, 0,'imageData');
?>
the following code is to upload the images in the database
<?php
$link = mysql_connect('localhost',
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('upload', $link);
if (!$db_selected) {
die ('Database error : ' . mysql_error());
}
$maxFileSize = "1000000";
$image_array = array("image/jpeg","image/
$fileType = $_FILES['userfile']['type'
$msg = '';
if(@$_POST['Submit'])
{
if (in_array($fileType, $image_array))
{
if(is_uploaded_file($_FILE
{
if($_FILES['userfile']['si
{
$imageData =addslashes (file_get_contents($_FILES
$sql = "INSERT INTO images ( imageData) VALUES ('$imageData')";
mysql_query($sql) or die(mysql_error());
$msg = " Data successfully uploaded";
}
else
{
$msg = ' Error : File size exceeds the maximum limit ';
}
}
}
else
{
$msg = 'Error: Not a valid image ';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<span style="color:#FF0000"><?ph
<h3>Select File to upload</h3>
<form enctype="multipart/form-da
<input name="userfile" type="file" />
<input type="submit" value="Submit" name="Submit"/>
</form>
</body>
</html>
i am able to upload the images here in the database but i am unable to display all the images that are stored in the database.help me out experts
BUT I would definetely recommend to store the images as files having a link in the database to the path where they are. The database can rapidly grow to big size. If that is just for learning purposes, the code provided should be ok.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window