Same file being uploaded when two have been chosen for upload

Dear Team,

I have PHP code where i have to upload compress 2 images at the same same. I save the two files as _frontid and _frontid. Upload is happening fine but the content is the same even when i choose different file. Any error i am doing

/*
 * Custom function to compress image size and 
 * upload to the server using PHP 
 */

function compressImage($source, $destination, $quality) {
    // Get image info 
    $imgInfo = getimagesize($source);
    $mime = $imgInfo['mime'];

    // Create a new image from file 
    switch ($mime) {
        case 'image/jpeg':
            $image = imagecreatefromjpeg($source);
            break;
        case 'image/png':
            $image = imagecreatefrompng($source);
            break;
        case 'image/gif':
            $image = imagecreatefromgif($source);
            break;
        default:
            $image = imagecreatefromjpeg($source);
    }

    // Save image 
    imagejpeg($image, $destination, $quality);

    // Return compressed image 
    return $destination;
}

function uploadfile($file, $uploadPath, $msisdn) {
    $status = 'error';
    if (!empty($_FILES[$file]["name"])) {
        // File info 
        $fileName = basename($_FILES[$file]["name"]);
        echo $imageUploadPath = $uploadPath . $fileName;
        $fileType = strtolower(pathinfo($imageUploadPath, PATHINFO_EXTENSION));

        // Allow certain file formats 
        $allowTypes = array('jpg', 'png', 'jpeg', 'gif');
        if (in_array($fileType, $allowTypes)) {
            // Image temp source 
            $imageTemp = $_FILES["frontid"]["tmp_name"];

            $imageUploadPath = $uploadPath.$msisdn . "_" . $file .".". $fileType;
            // Compress size and upload image 
            $compressedImage = compressImage($imageTemp, $imageUploadPath, 40);

            if ($compressedImage) {
                $status = 'success';
                $statusMsg = "Image compressed successfully.";
            } else {
                $statusMsg = "Image compress failed!";
            }
        } else {
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.' . $fileType;
        }
    } else {
        $statusMsg = 'Please select an image file to upload.';
    }
    return $statusMsg;
}

// File upload path 
$uploadPath = "uploads/";

// If file upload form is submitted 
$status = $statusMsg = '';
if (isset($_POST["submit"])) {
    
    $phone = 12345678;
    echo uploadfile("frontid", $uploadPath, $phone);
    echo uploadfile("backid", $uploadPath, $phone);
}
?>

the HTML part is here below

<form action="" method="post" enctype="multipart/form-data">    
    <input type="file" name="frontid" id="frontid" /><!-- comment -->
    <input type="file" name="backid" id="backid" />
    <input type="submit" name="submit" />
</form>```

Got the issues resolved. I had a static element in the function.

1 Like

The culprit seems to be this line:

$imageTemp = $_FILES["frontid"]["tmp_name"];

That should be

$imageTemp = $_FILES[$file]["tmp_name"];

Thanks @rpkamp . I appreciate your quick feedback. The issue is fixed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.