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

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!

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

I chose a user avatar upload for this example. I'm not sure what your image is, but the workflow should be similar. No duplicate avatars will be copied to the image path.

function get_avatar_filename($filename) {
  // only generate an avatar filename if the mimetype matches
  switch (mime_content_type($filename)) {
    case 'image/jpeg':
      return sprintf('%s.jpg', hash_file('md5', $filename));
    case 'image/gif':
      return sprintf('%s.gif', hash_file('md5', $filename));
    case 'image/png':
      return sprintf('%s.png', hash_file('md5', $filename));

    // otherwise the user uploaded a non-supported image
    // return the default image
    default:
      return 'default-avatar.jpg';
  }
}

function upload_avatar($avatarPath, $filename) {
  // get the avatar filename
  $f = get_avatar_filename($filename);

  // copy the file to $avarPath only if the file doesn't already exist
  if (!file_exists("{$avatarPath}/{$f}")) {
    move_uploaded_file($filename, "{$avatarPath}/{$f}");
  }

  // return the avatar filename
  return $f;
}

Now you can use these functions when you process the user form submission

// process user form submission ...
// ...

$filename = upload_avatar(
  $_SERVER['DOCUMENT_ROOT'] . "/stories/media/images/",
  $_FILES['user_avatar']['tmp_name']
);

// save the avatar location for the user ...
// or whatever
$user->setAvatar($filename);
$user->save();

If the user uploads a non-supported image type, they will just be assigned default-avatar.jpg which is a file that should exist in your images directory.