【问题标题】:Cant upload and store the image to the database by using php无法使用php将图像上传并存储到数据库
【发布时间】:2015-02-19 21:14:01
【问题描述】:

我尝试使用php构建一个上传图片系统,经过测试,系统回显“图片已上传”,但它没有显示在数据库中。这是我的代码在这里

上传.php

<?php
//connect database
include('config.php');

//file properties
 if(isset($_FILES['image'])){
   $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 "This is not an image";
    }
    else{
        $insert = "INSERT INTO image(name,picture) VALUES ('$image_name','$image')";

        if(!$insert){
            echo "Problem uploading";
        }
        else {
            $lastid =mysqli_insert_id($con);
            echo "Image Uploaded <p />Your Image :<p /><img src=get.php?id=$lastid>";
        }
    }
}


?>

get.php

<?php
include ('config.php');

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

$image = mysqli_query($con ,"SELECT * FROM image WHERE id=$id");
$image = mysqli_fetch_assoc($image);
$image = $image['picture'];

header("Content-type :image/jpeg");
?>

我点击提交按钮而不选择任何文件,这 2 行警告显示

警告:file_get_contents():upload.php 第 14 行中的文件名不能为空。

第 14 行是这个 $image = addlashes(file_get_contents($_FILES['image']['tmp_name']));

警告:getimagesize():upload.php 第 16 行中的文件名不能为空。

虽然第 16 行是这个 $image = addlashes(file_get_contents($_FILES['image']['tmp_name']));

对此有什么想法吗?

【问题讨论】:

  • 你为什么要将图像放入数据库中?
  • @MikeBrant 你是什么意思??
  • 我的意思是,一般来说,将图像存储在数据库中,而不是文件系统对图像的引用,是一种糟糕的应用程序设计。除非您对图像本身或类似的东西进行二进制搜索,否则您可能会为自己的架构不佳而苦恼。
  • @MikeBrant mike,我有任何参考来做你提到的文件系统参考吗??提供的任何参考都非常受欢迎..因为我是 php 语言的初学者..tq
  • 回头看我问的问题,现在我明白你的意思了。谢谢..哈哈

标签: php mysql image file-upload upload


【解决方案1】:

您需要一个函数来发送您的查询,否则您只需填写一个字符串:this:

$insert = "INSERT INTO image(name,picture) VALUES ('$image_name','$image')";

后面应该是这样的:

mysqli_query($con, $insert);

警告是由您的代码的多个问题引起的。首先,您正在检查文件是否以错误的方式上传:this

if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

将始终设置一个$file 变量,即使没有文件被选择到表单中,因此永远不会执行这个 if 语句:

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

并始终执行 else 块中的内容,这会导致错误,因为您提到的包含在此 else 块中的函数无法对任何文件进行操作。

因此,只需正确检查文件上传即可解决问题:一种方法是先将其删除,这是无用的

if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

然后改变这个:

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

到这里:

if(!isset($_FILES['image']['tmp_name'])){
    echo "Please select an image";
}

【讨论】:

  • 那么这里如何处理“mysqli_query($con,$insert)”??做一个“if/else”语句来比较它还是什么?介意解释更多吗? @kos
  • 你可以像这样比较函数本身: if(!mysqli_query($con, $insert)) echo "Problem uploading"; else echo "图片上传

    你的图片:

    ";
  • 哇..现在可以工作了..图像现在可以插入数据库了..谢谢
  • 但警告警告:file_get_contents(): Filename cannot be empty in upload.php line 14.Warning: getimagesize(): Filename cannot be empty in in upload.php line 16. this 2 line错误仍然存​​在..介意给我一些想法吗?谢谢@kos
  • if(!isset($_FILES['image']['tmp_name'])),因为另一种方式是检查是否已发送任何 POST 数据。因此,填写到表单中的任何输入都会使 isset($_POST) 返回 1,而用户可能没有选择任何图像,所以如果表单中有多个输入,这是唯一正确的方法。如果你只有一个输入 isset($_POST) 没关系。很高兴听到它有帮助,我会相应地改变我的答案
【解决方案2】:

在第 16 行之后,您需要回显图像数据。

回显 $image;

@ken,这是我用来通过设置正确的标题来输出图像的函数:

    function output_headers($filetype, $imgsize, $filename, $attachment=false)
{
header("Content-Type: {$filetype}");
header("Pragma: public");
header("Cache-Control: public");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * 4 -1;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
header("Cache-Control: max-age=$offset");
//header("Content-Type: application/octet-stream");
header('Accept-Ranges: bytes');
header("Content-Length: $imgsize");
#insert the filename we want into the image so it can be saved with something
# more meaningful that showimg.jpg. :-)
# 04/03/06
#attachment causes save as to appear, inline serves imbedded image.
if ($attachment)
    header('Content-Disposition: attachment; filename="' . $filename . '"');
else
    header('Content-Disposition: inline; filename="' . $filename . '"');
}

这是一个名为 showimg.php.jpg 的 php 文件的一部分

这需要与以下 .htaccess 文件一起保存到单独的目录中,以让 php 处理对 .jpgs 的请求:

AddType application/x-httpd-php .jpg

这样网页就引用了图片&lt;img src='/img/showimg.php.jpg?&lt;add your parameters here&gt;

问候 史蒂夫

【讨论】:

    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2015-09-18
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多