dow98764 2016-07-22 00:30
浏览 23

防止服务器上的图像重复

I have a form where the user enters data and uploads an image. This image will be checked if there is an image that is exactly the same using md5 hashing. Each image that is uploaded will have its own md5 hash code. If a user decides to upload an image that is exactly like the one on the server, then that image will not be moved. Instead, when creating the entry, the image will inherit the name of that file from the different entry with the same hash code. But I am running into several problems with my current code. For one, when the user uploads an image for the first time, there is no hash code. Another problem I am experiencing with my code is that even when I upload an image with the same hash code, the name of the image is changing to a uniqid. It is executing the else block and not the if block.

Here's my code:

PHP

if (isset($_POST["pageNum"], $_FILES["image"], $_POST["subtitle"], $_POST["text"]))
    {
        $page = $_POST["pageNum"];
        $url = $_SESSION["articleUrl"];
        $subtitle = filter_data($_POST["subtitle"]);
        $text = filter_data($_POST["text"]);

        $name = $_FILES["image"]["name"];
        $tempName = $_FILES["image"]["tmp_name"];

        $target_file = $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/$name";

        $hash = md5_file($target_file);
        $resultHash = $db->query("SELECT * COUNT(*) FROM `Stories` WHERE hash = '$hash' LIMIT 1");

        if ($resultHash->num_rows > 0)
        {
            $row = $resultHash->fetch_array();
            $name = $row["image"];

        }

        else
        {
            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);
        }

        $result = $db->query("SELECT * FROM Stories WHERE page = '$page' AND url = '$url'");

        if ($result->num_rows == 0)
        {
            $db->query("INSERT INTO `Stories` (`image`, `text`, `url`, `subtitle`, `page`, `hash`) VALUES ('$name', '$text', '$url', '$subtitle', '$page', '$hash')");
        }

        else
        {
            $db->query("UPDATE Stories SET image = '$name', text = '$text', url = '$url', subtitle = '$subtitle', page = '$page', hash = '$hash' WHERE url = '$url' AND page = '$page'");
        }

    }
  • 写回答

1条回答 默认 最新

  • dras2334 2016-07-22 00:43
    关注

    The lines below are problematic because you will only get a hash if the $target_file already exists. If there is no file by that name, then there is nothing to hash and you can't get a hash to compare against the DB value.

    $target_file = $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/$name";
    $hash = md5_file($target_file);
    

    The first line is useless; remove it. You should be calculating the hash of the newly uploaded file instead because that's what needs to be compared to the hashes stored in the DB:

    $hash = md5_file($tempName);
    

    Later, you also need to change your getimagesize check to work with the newly uploaded file because this is the file that needs to be processed (we get to this check if this is a new, unique file):

    if (getimagesize($tempName) == true)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?