不能使用php上传和存储图像到数据库 [英] Cant upload and store the image to the database by using php

查看:158
本文介绍了不能使用php上传和存储图像到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to build an upload image system by using php.After testing it,the system echo out"Image is uploaded",but it doesn't shown in database.Here is my code here

upload.php

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

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

and I clicking the submit button without choosing any files this 2 line of warning is showing up


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

Warning: file_get_contents(): Filename cannot be empty in upload.php line 14.

line14是这个$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));

line14 is this $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

警告:getimagesize():文件名不能是在upload.php第16行清空。

Warning: getimagesize(): Filename cannot be empty in in upload.php line 16.

第16行是$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name'])) ;

while line 16 is this $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

对此有何看法?

推荐答案

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

You need a function to send your query, otherwise you just filled up a string: this:

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

后面应该是:

mysqli_query($con, $insert);

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

The warnings are caused by multiple issues with your code. First you are checking whether the file has been uploaded in the wrong way: this

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

将始终设置 $ file 变量,即使没有在表单中选择任何文件,因此导致永远不会执行此if语句:

Will always set a $file variable, even though no file has been selected into the form, leading therefore to never execute this if statement:

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

并且始终执行else块中的内容,这会导致错误,因为你提到的这个函数包含在这个else块中,所以不能对任何文件进行操作。

and to always execute what's in the else block instead, which causes the errors, because the functions you mentioned, which are contained in this else block, are not able to operate on any file.

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

Therefore simply checking the file upload correctly will solve the issue: one way to do this would be to first remove this, which is unuseful

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

这篇关于不能使用php上传和存储图像到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆