开发者

Getting an image from a database using PHP

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");

?>


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.


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


You are missing a line after the header output

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


Very quick glance, in get.php change:

$image=$image('image');

to

$image=$image['image'];

mysql_fetch_assoc() converts the results into an array.


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);


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜