php - 使用 PHP 从数据库获取图像

标签 php mysql database upload insert

第一次使用堆栈溢出。

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

这是 YouTube 教程的链接

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

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

其中包含文件

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

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


?>

最后连接

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

?>

最佳答案

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

附注不要使用 addslashes 来防止 SQL 注入(inject)。使用mysql_real_escape_string .

关于php - 使用 PHP 从数据库获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154664/

相关文章:

php - 从 WordPress 页面提交表单无法正常工作

java - 从数据库中删除项目 - Android Studio 中的 ListView

java - 递归计算它有多少级别和 child 数量

database - 外键到底是什么?

php - 模块化站点中的上下文感知 AJAX 调用

php - eZ Publish 6 (eZPlatform) 连接问题

php - 使用 php 的 PDOStatement::execute/fetch/rowCount 测试数据库中值是否存在存在问题?

php - 在 codeigniter 中查看用户数据和个人资料图片?

PHP:PDOStatement 简单的 MySQL Select 不起作用

php - 如何将 LIKE %% 应用于变量?