dongshi4078 2016-07-21 21:01
浏览 12

检测用户是否正在上传相同的图像

I have a form that contains an image upload and an input text box. The user will be able to upload an image and enter text without refreshing the page using Ajax. The image will be relayed to PHP and PHP will handle what to do with the image. My problem is that for the first time the user uploads an image, it'll be checked if the same image name is on the server or not. If it is, the image name will get a uniqid() and then will be uploaded. But what if the user changes the data in the text box field, but keeps the image? Then that image will be uploaded again with a uniqid() since it's already on the server. I've tried solving this using my current code for the image handling:

PHP

$target_file = $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/$name";
        if (isset($_SESSION["size"]))
        {
            $prevSize = $_SESSION["size"];

            if (filesize($prevSize) != filesize($size))
            {
                if (@getimagesize($target_file) == true)
                {
                    $ext = pathinfo($name, PATHINFO_EXTENSION);     
                    $name = basename($name, "." . $ext);
                    $name = $name . uniqid() . "." . $ext;
                    $target_file = $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/$name";
                }
            }

        }

        else
        {
            $_SESSION["size"] = $size;

            if (@getimagesize($target_file) == true)
            {
                $ext = pathinfo($name, PATHINFO_EXTENSION);     
                $name = basename($name, "." . $ext);
                $name = $name . uniqid() . "." . $ext;
                $target_file = $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/$name";
            }
        }




        move_uploaded_file($tempName, $target_file);

Unfortunately, this code isn't working like I want it to. If I upload the same image twice in a row, in the same session, it doesn't override my previous image. Instead, it puts it on the server with a uniqid name. What am I doing wrong? And if there's a better way in solving this, I'd love to know!

  • 写回答

2条回答 默认 最新

  • douxian1892 2016-07-21 21:13
    关注

    What you can do is whenever someone uploads an image, store a hash of the image, encrypt it and store it in the database on the image row. From now on, whenever someone uploads an image run a query like this: SELECT COUNT(*) FROM images WHERE hash = $hash then in an if statement check if the returned value is bigger than 0, if it is, do what you need to do without re-uploading the image, and if it is 0, then upload your image and proceed

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害