Solved

display images in php

Posted on 2013-01-01
3
288 Views
Last Modified: 2013-01-16
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","image/gif","image/bmp","image/pjpeg","image/png");

$fileType = $_FILES['userfile']['type'];

 
$msg = '';


if(@$_POST['Submit'])
{


if (in_array($fileType, $image_array))
{


 if(is_uploaded_file($_FILES['userfile']['tmp_name']))
 {
 
 
        if($_FILES['userfile']['size'] < $maxFileSize)
            {
 

                  $imageData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
 
        $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"><?php echo $msg; ?></span><br />

        <h3>Select File to upload</h3>
 
        <form enctype="multipart/form-data" 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
0
Comment
Question by:codeoxygen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38735888
Try to display it like this:

$db_selected = mysql_select_db('upload', $link);

if (!$db_selected) {
    die ('Database error : ' . mysql_error());
}

        $sql = "SELECT * FROM images";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        
        header("Content-type: image/jpeg");
        echo $row['imageData'];
 
     
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38735896
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.
0
 
LVL 9

Accepted Solution

by:
rinfo earned 500 total points
ID: 38735970
You have mentioned
$sql = "INSERT INTO images ( imageData) VALUES ('$imageData')";
besides you have also mentioned
  $sql = "SELECT * FROM images";
which indicates you have just one field imageData in the images table - of course there could be an id field.
Does it also mean you have only one image in the images tables.
Because you have mentioned
   $sql = "SELECT * FROM images";
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0,'imageData');
if there are multiple rows in the table you have to iterate through the result array to
display images one by one.
I think this is where you are failing.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
css issues with IE 11 8 65
WordPress Madness 8 54
RSS FEED - PROPERLY FORMATTED URL 2 60
htaccess block all countries but the US 4 99
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question