首页 文章

不能使用php上传和存储图像到数据库

提问于
浏览
1

我尝试使用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");
?>

然后单击提交按钮而不选择任何文件,这两行警告显示出来

警告:file_get_contents():在upload.php第14行中,文件名不能为空 . 行14是$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));警告:getimagesize():在upload.php第16行中,文件名不能为空 . 而第16行是$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));

对此有何想法?

2 回答

  • 1

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

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

    应该遵循这个:

    mysqli_query($con, $insert);
    

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

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

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

    echo $ image;

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

    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
    

    通过这种方式,网页引用图像 <img src='/img/showimg.php.jpg?<add your parameters here>

    对史蒂夫说

相关问题