2

first time using stack overflow.

I have followed the following 2 part youtube tutorial on uploading/storing an image in a MYSQL database. I have followed the instructions but my image is not appearing for me. I use connect.php to connect to the database, this appears to be working fine. It seems the problem is with get.php as when I test echoing any images from it I always get no image. used phpmyadmin to create the database and am using xampp.

here is the link to the youtube tutorials

http://www.youtube.com/watch?v=CxY3FR9doHI

http://www.youtube.com/watch?v=vFZfJZ_WNC4&feature=fvwrel

Included are the files

<html>
<head>
    <title>Upload an 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

include 'connect.php';



//file properties
$file = $_FILES['image']['tmp_name'];

if(!isset($file))
    echo "Please select an image.";
else{

    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name=addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if ($image_size==FALSE)
        echo "That's not an image.";
    else{
            if(!$insert = mysql_query("INSERT INTO store       VALUES('','$image_name','$image')"))
        echo"Problem uploading image";
    else{
        $lastid = mysql_insert_id();

        echo "image uploaded.<p />your image:<p /><img src=get.php?id=$lastid>";
        }
    }
}

?>
</body>
</html>

Here is get.php

<?php
include 'connect.php';

$id=stripslashes($_REQUEST('id'));
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image=$image('image');

header("content-type: image/jpeg");


?>

And finally connect

<?php
// connect to database

$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="test";

@mysql_connect("$db_host","$db_username","$db_pass") or die("Could not connect to      mysql");
mysql_select_db("$db_name")or die("Cant find database");

?>
3
  • I see that you're suprsessing errors in at least one place (the @ symbol). Are you checking return values at all? Checking your error log? Commented May 27, 2011 at 15:49
  • 1
    $_REQUEST('id') should be $_REQUEST['id'] as well Commented May 27, 2011 at 16:03
  • 1
    +1 for a good first question that included all relevant code and with a good amount of information. Commented May 27, 2011 at 17:13

6 Answers 6

4

Your get.php doesn't echo $image.
Also $image=$image('image'); should be $image=$image['image'];, and $_REQUEST('id') should be $_REQUEST['id'].

P.S. Don't use addslashes to prevent against SQL injections. Use mysql_real_escape_string.

Sign up to request clarification or add additional context in comments.

Comments

1

You never echo the image data in get.php, so you're serving a blank 0-byte image.

Comments

1

You are missing a line after the header output

header("content-type: image/jpeg");
echo $image;

Comments

0

Very quick glance, in get.php change:

$image=$image('image');

to

$image=$image['image'];

mysql_fetch_assoc() converts the results into an array.

1 Comment

-1 There's enough code to justify more than a "quick glance", if that's all the time you can spare then why answer the question?
0

You better of bas64_encoding an decoding that way none ansi chars wont create a problem.

base64_encode(file_get_contents($_FILES['image']['tmp_name']));

This is wrong as array is [] not () include 'connect.php';

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

header("content-type: image/jpeg");
echo base64_decode($image);

Comments

-2

Piece of code I use in a site of mine:

<?php
ob_start();
require_once("db.php.lib");

DBLogin();
$sql = "select pic_user_id, pic_full_data as bindata, pic_full_mime as mime, pic_full_size as size from pics where pic_name = '".urldecode($_GET["pic_name"])."'";

$result = DBExec($sql);
if ($result)
{
  $row = DBGetNextRow($result);
  if ($row)
  {
    header("Content-type: ".$row["mime"]);
    header("Content-length: ".$row["size"]);
    ob_clean();
    echo $row["bindata"];
    ob_end_flush();
  }
}
?>

It looks like you're leaving out the actual output of the image data, and the length might be required by some browsers...

1 Comment

Sorry but how does this help the user, that means nothing to him when he has got his completely different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.