Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

PHP通过保存路径达到上传图片的效果,已实现了回显,对数据库图片修改产生了问题

html
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图片显示</title>
    <script src="jquery-3.3.1.js"></script>
    <style type="text/css">
        .div_imgall {border:1px solid #cccccc;width:120px;height:120px;position:relative;margin-top:20px;margin-left:20px;}
        .input_flie {display:block;width:100%;height:100%;opacity:0;z-index:999;position:absolute;left:0;top:0;}
        .div_shuline {position:absolute;width:50%;height:2%;background-color:#cccccc;left:25%;top:49%;z-index:1;}
        .div_hengline {position:absolute;width:2%;height:50%;background-color:#cccccc;left:49%;top:25%;z-index:1;}
    </style>
</head>
<body>
    <center>
        <h2>文件上传</h2>
        <!-- 文件上传得form表单 -->
        <form action="upload_update.php" method="post" enctype="multipart/form-data" id="update">
            上传图片:
            <div class="div_imgall">
                <input type="file" class="input_flie" name="pic" rel="file" id="up_pic"/>
                <div class="jiahao">
                    <div class="div_shuline"></div>
                    <div class="div_hengline"></div>
                </div>
                <img id="preview" src="" style="position: absolute;width: 110px;height: 110px;padding: 5px 12px 12px 9px;margin-left: -64px;z-index: 6;"/>
            </div>
            <div class="div_imgall">
                <input type="file" class="input_flie" name="pic2" rel="file" id="up_pic2"/>
                <div class="jiahao">
                    <div class="div_shuline"></div>
                    <div class="div_hengline"></div>
                </div>
                <img id="preview2" src="" style="position: absolute;width: 110px;height: 110px;padding: 5px 12px 12px 9px;margin-left: -64px;z-index: 6;"/>
            </div>
        </form>
        <div>
            <button type="button" name="button" onclick="update_btn();" style="margin-top: 10px;margin-left: 15px;">更新</button>
        </div>
    </center>
    <script type="text/javascript">
        //提交from表单
        function update_btn(){
            document.getElementById("update").submit();
        }
        //图片上传
        var inp=document.getElementById("up_pic")
        inp.onchange=function(){
            var file=this.files[0];
            console.log(file);
            imageUrl = URL.createObjectURL(file);
            image = document.getElementById("preview");
            image.src = imageUrl;
        }
        var inp2=document.getElementById("up_pic2")
        inp2.onchange=function(){
            var file=this.files[0];
            console.log(file);
            imageUrl = URL.createObjectURL(file);
            image2 = document.getElementById("preview2");
            image2.src = imageUrl;
        }
        $(document).ready(function(){
            $.ajax({
                url : "./getimage.php",//后台请求的数据,用的是PHP
                type : "post",//请求方式
                async : false,//是否异步请求
                success : function(data) {?//如果请求成功,返回数据。
                    var res = JSON.parse(data);
                    var img1 = res.data[0].path;
                    var img2 = res.data[0].path_t;
                    $("#preview").attr('src',img1);
                    $("#preview2").attr('src',img2);
                },
            })
        })
    </script>
</body>
</html>
upload_update.php
<?php
    //1.获取上传文件信息
    $upfile=$_FILES["pic"];
    $upfile2=$_FILES["pic2"];
    //定义允许的类型
    $typelist=array("image/jpeg","image/jpg","image/png","image/gif");
    $path="./upfiles/";//定义一个上传后的目录
    //2.过滤上传文件的错误号
    if($upfile["error"]>0){
        switch($upfile['error']){//获取错误信息
                case 1:
                    $info="上传得文件超过了 php.ini中upload_max_filesize 选项中的最大值.";
                    break;
                case 2:
                    $info="上传文件大小超过了html中MAX_FILE_SIZE 选项中的最大值.";
                    break;
                case 3:
                    $info="文件只有部分被上传";
                    break;
                case 4:
                    $info="没有文件被上传.";
                    break;
                case 5:
                    $info="找不到临时文件夹.";
                    break;
                case 6:
                    $info="文件写入失败!";break;
        }die("上传文件错误,原因:".$info);
    }
    if($upfile2["error"]>0){
        switch($upfile2['error']){//获取错误信息
                case 1:
                    $info2="上传得文件超过了 php.ini中upload_max_filesize 选项中的最大值.";
                    break;
                case 2:
                    $info2="上传文件大小超过了html中MAX_FILE_SIZE 选项中的最大值.";
                    break;
                case 3:
                    $info2="文件只有部分被上传";
                    break;
                case 4:
                    $info2="没有文件被上传.";
                    break;
                case 5:
                    $info2="找不到临时文件夹.";
                    break;
                case 6:
                    $info2="文件写入失败!";break;
        }die("上传文件错误,原因:".$info2);
    }
    //3.本次上传文件大小的过滤(自己选择)
    if($upfile['size']>2000000){
        die("上传文件大小超出限制");
    }
    if($upfile2['size']>2000000){
        die("上传文件大小超出限制");
    }
    //4.类型过滤
    if(!in_array($upfile["type"],$typelist)){
        die("上传文件类型非法!".$upfile["type"]);
    }
    if(!in_array($upfile2["type"],$typelist)){
        die("上传文件类型非法!".$upfile2["type"]);
    }
    //5.上传后的文件名定义(随机获取一个文件名)
    $fileinfo=pathinfo($upfile["name"]);//解析上传文件名字
    do{ 
        $newfile=date("YmdHis").rand(1000,9999).".".$fileinfo["extension"];
    }while(file_exists($path.$newfile));
    $fileinfo2=pathinfo($upfile2["name"]);//解析上传文件名字
    do{ 
        $newfile2=date("YmdHis").rand(1000,9999).".".$fileinfo2["extension"];
    }while(file_exists($path.$newfile2));
    //6.执行文件上传
    //判断是否是一个上传的文件
    if(is_uploaded_file($upfile["tmp_name"])){
        //执行文件上传(移动上传文件)
        if(move_uploaded_file($upfile["tmp_name"],$path.$newfile)){
            if(move_uploaded_file($upfile2["tmp_name"],$path.$newfile2)){
                echo "图片更新成功!";
                include('config.php');
                //将图片的名称和路径存入数据库
                $sql = "UPDATE image3(path,path_t) SET path='$path$newfile', path_t='$path$newfile2' where id";
                $result = mysqli_query($conn,$sql);
                if($result){
                    echo"图片已更新到数据库";
                } else {
                    echo"请求失败,请重试";
                }
            } else {
                die("更新图片失败!");
            }
        }else{
            die("没有获取到上传的图片!");
        }
    }
?>

html效果页面:
image
如图所示,已能获取数组对应的图片并回显了出来。

想要对数据库保存的图片进行修改
image

PHP后台报错了,报:
上传文件错误,原因:没有文件被上传.

说明$upfile没有获取到本身input容器里面就有的图片和$upfile2没有获取到新上传的图片,请问这里该如何进行获取?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

PHP 支持同时上传多个文件并将它们的信息自动以数组的形式组织
对超全局变量 $_files 有误解,它在传单文件或多文件时结构有所不同,参阅。简而言之,你需要关注以下两点

  1. 多文件上传传递的是一个数组
  2. 修改已上传图片,需要知悉原图的数据库id,根据id的唯一性确定数据库修改记录

多文件上传下的$_files结构如下

Array
(
    [name] => Array(
        [0] => foo.txt
        [1] => bar.txt
 )
    [type] => Array(
        [0] => text/plain
        [1] => text/plain
 )
 ....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...