php上传图片至数据库并显示于网页

php上传图片到数据库并于网页中显示

sql


```sql
CREATE TABLE test_img (
    id int(4) UNSIGNED NOT NULL AUTO_INCREMENT,
    path varchar(100) default NULL,
    upload_time  timestamp default CURRENT_TIMESTAMP,
    PRIMARY KEY(id)
)engine=myisam DEFAULT charset=utf8

img.html


<!DOCTYPE html><html lang="utf-8"><head>

    <meta charset="UTF-8">

    <title>图片上传</title></head><body><form action="img.php" method="post" enctype="multipart/form-data">

    选择上传的图片: <input type="file" name="file" accept="image/*">

    <br><br>

    <input type="submit" value="上传"></form>

img.php


<?php
header("Content-Type: text/html;charset=utf-8");

$conn = new mysqli('localhost', 'root', '123', 'aty',3306);

if (!$conn) {

    die("Connection failed: " . mysqli_connect_error());
}
$destination = 'C:\Users\image';

$file        = $_FILES['file']; // 获取上传的图片

$filename    = $file['name'];

$insert = "INSERT INTO test_img (path) VALUES ('$filename')";

$test   = move_uploaded_file($file['tmp_name'], $destination . iconv("UTF-8", "gb2312", $filename));



if ($insert && $test) {

    $conn->query($insert);

} else {

    echo '上传失败' . '<br>';

}
$select = 'SELECT path FROM test_img';

$result = $conn->query($select);

while ($row = $result->fetch_assoc()) {

    echo "<img src=" . $destination . $row['path'] . ">";

}




运行结果及报错内容

img

我不理解我照着代码抄下来依然报错,希望能够指点一下

原代码网页https://www.php.cn/php-weizijiaocheng-455441.html

$imgsetpath="F:/website/upload/";//图片保存的路径,这个主要是看你图片将来是要用什么现实,如果是要二级域名来显示图片,存在哪都可以,如果是用主域名显示,只能保存再站点的根目录下的文件夹upload里

header("Content-Type: text/html;charset=utf-8");
$conn = new mysqli('localhost', 'root', '123', 'aty',3306);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

}
$picurl=$_FILES["file"]["name"];
if($picurl==''){
    exit("请上传图片!");
}
$core_filetype='.jpg|.jpge|.gif|.png';
$img_type=array("image/gif","image/jpeg","image/jpg","image/pjpeg","image/x-png","image/png");
$core_filesize=400;
if($picurl!=""){
    $fileextensionname=strtolower(preg_replace("/.+(\..+)/i","\\1",$picurl));
    $filename = $deal_date.'_'.date('YmdHis').$fileextensionname;
    if($_FILES["file"]["error"]>0){
        exit("图片上传失败!");    
    }elseif(!preg_match("/".$core_filetype."/i",$fileextensionname)){
        exit("图片上传失败:上传的文件类型不被容许!");    
    }elseif(!in_array($_FILES["file"]["type"],$img_type)){
        exit("图片上传失败:上传的文件类型不被容许!!");    
    }elseif(($_FILES["file"]["size"]/1024)>$core_filesize){
        exit("图片上传失败:上传的文件大小超过".$core_filesize."KB限制!");    
    }else{
        $imgsetpath="F:/website/upload/";//图片保存的路径,这个主要是看你图片将来是要用什么现实,如果是要二级域名来显示图片,存在哪都可以,如果是用主域名显示,只能保存再站点的根目录下的文件夹upload里
        $directory=date('Y/m');
        if(!file_exists($imgsetpath.$directory)){
            if (mkdir($imgsetpath.$directory, 0777, true)){//目前不存在就创建
                chmod($imgsetpath.$directory, 0777 );
            }else{
                exit("图片上传失败,创建文件夹失败 ".$imgsetpath.$directory."!");    
            }
        }
            
        if(copy($_FILES["file"]["tmp_name"],$imgsetpath.$directory."/".$filename)){
        }else{
            exit("文件大小:".(round($_FILES["file"]["size"]/1024, 2))."KB图片上传失败!!!!");    
        }
        $picurl="/upload/".$directory."/".$filename;//把这个图片地址存进数据库
    }
}
$insert = "INSERT INTO test_img (path) VALUES ('$picurl')";
$conn->query($insert);
 $select = 'SELECT path FROM test_img';
$result = $conn->query($select);
while ($row = $result->fetch_assoc()) {
    echo "<img src=" . $row['path'] . ">";
}