3 Replies - 395 Views - Last Post: 04 November 2012 - 04:20 PM Rate Topic: -----

#1 ajthewhiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

issue displaying images in php

Posted 03 November 2012 - 11:23 PM

Hello,

I am having trouble displaying the actual image when uploading. Everything works fine up until that point. Also I am encountering some errors when testing without uploading. I am getting warning message saying that when using both the file_get_content() and getimagesize() that the filename cannot be empty. I do not see where the filename is empty on my code because it isn't. I have include the error_reporting to eliminate it but it doesn't seem to work. Below is my code for you to view on why it is not displaying the actual image when uploading.

//this is the index page
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Upload image</title>
    </head>
    <body>
        <form action="index.php" method="post" enctype="multipart/form-data">
            File:
            <input type="file" name="image"/>
            <input type="submit" value="Upload"/>            
        </form>
        
       <?php 
            //prompt error reporting
            error_reporting(E_ALL ^ E_NOTICE);            
            //connect to database
            mysql_connect("localhost", "root", "") or die(mysql_error());
            mysql_select_db("upload") or die(mysql_error());
            
            //file properties
            $file = $_FILES['image']['tmp_name'];
            
            if(!isset($file))
            {
                print "Select a image";
            }
            else
            {
               $image       = addslashes(file_get_contents($_FILES['image']['tmp_name']));
               $image_name  = addslashes($_FILES['image']['name']);
               $image_size  = getimagesize($_FILES['image']['tmp_name']);
               
               //test to see if an image is select
               if($image_size == FALSE)
               {
                   print "No image";                   
               }               
               else
               {
                   if(!$insert = mysql_query("INSERT INTO image VALUES('', '$image_name', '$image')"))
                   {
                       print "Problem uploading";
                   }
                   else
                   {
                       $lastID = mysql_insert_id();
                       print "<img src=get.php?id='$lastID'";
                   }
               }
            }
       ?> 
    </body>
</html>



and this is the get.php page
<?php
//prompt error reporting
error_reporting(E_ALL ^ E_NOTICE);
//connect to database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("upload") or die(mysql_error());

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM image WHERE id = '$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpg");

print $image;

?>




in addition I have uploaded the warning message that I have mentioned earlier.Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: issue displaying images in php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: issue displaying images in php

Posted 04 November 2012 - 02:01 AM

Hey.

Consider what happens the first time that page is opened, before you submit the form. What is the value of $_FILES['image']['tmp_name'] going to be then?

You always want to make sure user input actually exists before you try to use it. You can use the empty() or isset() to do that.
Was This Post Helpful? 0
  • +
  • -

#3 ajthewhiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

Re: issue displaying images in php

Posted 04 November 2012 - 02:07 PM

View PostAtli, on 04 November 2012 - 02:01 AM, said:

Hey.

Consider what happens the first time that page is opened, before you submit the form. What is the value of $_FILES['image']['tmp_name'] going to be then?

You always want to make sure user input actually exists before you try to use it. You can use the empty() or isset() to do that.


Ok I got that to fix, but what about the actual image displaying? How can I get that to show when uploading?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: issue displaying images in php

Posted 04 November 2012 - 04:20 PM

Before I go into that, there is one question that I should probably ask. Why are you storing the file in the database? The best way to store a file is to put the actual file on the hard drive, and then put the location of the file into the database. The file-system is much more efficient at storing and retrieving large binary objects than a relational database is. It also simplifies the method used to serve the file somewhat. At it's simplest, you can just have the HTTP server serve the file directly.


With that said, there are a couple of things you should look into in regards to making your current code work.

  • The <img> tag in your "index.php" file is all messed up.
    // This is what your version will end up looking like:
    <img src=get.php?id='123'
    
    // It should look more like:
    <img src="get.php?id=123" alt="Uploaded image">
    
    

    (Note that the "alt" attribute is not optional for the img tag, if you want to create valid HTML.)

  • In the code that adds the image to the database, use use the addslashes function a lot. That is not enough. In order to properly transfer user input to MySQL, especially if it contains binary data, you must either use prepared statements (MySQLi and PDO only), or you must pass it through mysql_real_escape_string function. (Don't let the name fool you. In PHP, binary data is usually stored as normal character strings.)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1