使用PHP从数据库获取图像

时间:2022-09-25 16:39:08

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.

我已经按照以下2部分关于在MYSQL数据库中上传/存储图像的youtube教程。我按照说明操作,但我的图像没有显示给我。我使用connect.php连接到数据库,这似乎工作正常。似乎问题在于get.php,因为当我测试回显来自它的任何图像时,我总是得不到图像。使用phpmyadmin创建数据库并使用xampp。

here is the link to the youtube tutorials

这是youtube教程的链接

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

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

?>

6 个解决方案

#1


4  

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

你的get.php不会回显$ image。另外$ image = $ image('image');应该是$ image = $ image ['image'];,而$ _REQUEST('id')应该是$ _REQUEST ['id']。

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

附:不要使用addslashes来防止SQL注入。使用mysql_real_escape_string。

#2


1  

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

你永远不会在get.php中回显图像数据,所以你要提供一个空白的0字节图像。

#3


1  

You are missing a line after the header output

标题输出后缺少一行

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

#4


0  

Very quick glance, in get.php change:

快速浏览,在get.php中更改:

$image=$image('image');

to

$image=$image['image'];

mysql_fetch_assoc() converts the results into an array.

mysql_fetch_assoc()将结果转换为数组。

#5


0  

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

你更好的bas64_encoding解码,没有ansi chars不会产生问题。

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

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

这是错误的,因为数组是[] not()包括'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);

#6


-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


4  

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

你的get.php不会回显$ image。另外$ image = $ image('image');应该是$ image = $ image ['image'];,而$ _REQUEST('id')应该是$ _REQUEST ['id']。

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

附:不要使用addslashes来防止SQL注入。使用mysql_real_escape_string。

#2


1  

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

你永远不会在get.php中回显图像数据,所以你要提供一个空白的0字节图像。

#3


1  

You are missing a line after the header output

标题输出后缺少一行

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

#4


0  

Very quick glance, in get.php change:

快速浏览,在get.php中更改:

$image=$image('image');

to

$image=$image['image'];

mysql_fetch_assoc() converts the results into an array.

mysql_fetch_assoc()将结果转换为数组。

#5


0  

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

你更好的bas64_encoding解码,没有ansi chars不会产生问题。

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

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

这是错误的,因为数组是[] not()包括'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);

#6


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

看起来你要省略图像数据的实际输出,某些浏览器可能需要长度......