重复由不同用户同时上传的文件php

时间:2019-02-07 16:00:42

标签: php mysql

我有以下问题: 当用户指向不同的属性时,来自一个属性的照片可以显示在另一属性的照片上。我注意到有时属性错误的图片具有正确属性的图片,有时它们消失了,但我不知道如何解决此问题。有人知道如何解决这个问题吗?

 if(is_array($_FILES)) {
        $br = count($_FILES['image']['name']);
        echo $br;
       foreach($_FILES['image']['tmp_name'] as $key=>$val)
        {
            $br--;

            $sql3 = "SELECT id FROM property_images ORDER BY id DESC LIMIT 1";  

            $result3 = $conn->query($sql3);
            $row3 = $result3->fetch_assoc();

            $file = $_FILES['image']['tmp_name'][$key];
            $sourceProperties = getimagesize($file);
            $fileNewName = $row3['id'] + 1;
            $folderPath = 'properties_pic/'.$date.'/';
            $ext = pathinfo($_FILES['image']['name'][$key], PATHINFO_EXTENSION);
            $imageType = $sourceProperties[2];
            $sql3 = "SELECT id FROM property_images ORDER BY id DESC LIMIT 1";

                $result3 = $conn->query($sql3);
                $row3 = $result3->fetch_assoc();

            switch ($imageType) {


                case IMAGETYPE_PNG:
                    $imageResourceId = imagecreatefrompng($file);
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    $file=imagepng($targetLayer,$folderPath. $fileNewName. ".jpeg");
                    break;

                case IMAGETYPE_GIF:
                    $imageResourceId = imagecreatefromgif($file); 
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    $file=imagegif($targetLayer,$folderPath. $fileNewName. ".jpeg");
                    break;


                case IMAGETYPE_JPEG:
                    $imageResourceId = imagecreatefromjpeg($file); 
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    $file=imagejpeg($targetLayer,$folderPath. $fileNewName. ".jpeg");
                    break;


                default:
                    echo "Invalid Image type.";
                    exit;
                    break;
            }
                    if($file)
                    {
                        //Image db insert sql


                        $fileNewName = $fileNewName.'.jpeg';
                        $sql2 = "INSERT INTO property_images (id_property, name_image, folder_image, date_upload, user_id) VALUES ('$id', '$fileNewName', '$date', '$date2', '$user_id')";

                        if ($conn->query($sql2) === TRUE)
                        {
                             $sql44 = "UPDATE properties SET pic='1' WHERE id=$id";
                             $conn->query($sql44) === TRUE;

                            echo "<meta http-equiv='refresh' content='0'>";
                        }


                    }

        }   

    }
}

}
function imageResize($imageResourceId,$width,$height) {


if($width>$height)
{
    $targetWidth = $width;
    $targetHeight = $height;
    while($targetWidth>800)
    {
        $targetWidth = $targetWidth/1.1;
    }
    while($targetHeight>600)
    {
        $targetHeight = $targetHeight/1.1;
    }

}
else
{
    $targetWidth = $width;
    $targetHeight = $height;
    while($targetWidth>600)
    {
        $targetWidth = $targetWidth/1.1;
    }
    while($targetHeight>800)
    {
        $targetHeight = $targetHeight/1.1;
    }
}

    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);


    return $targetLayer;
}

0 个答案:

没有答案