A flexible function to upload images in PHP


I have created a function that I use to upload my images in PHP

function addImage($imageWidthRequired, $imageHeightRequired) {

    if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
        $imageSize = @getimagesize($_FILES['image']["tmp_name"]);

        $imageWidth = $imageSize['0'];
        $imageHeight = $imageSize['1'];

        if ($imageWidth == $imageWidthRequired && $imageHeight == $imageHeightRequired) {

            //Perform an action

Everything works fine but I am now facing a problem. I want the user to be able to upload an image without specifying the required dimensions$imageWidthRequired and$imageHeightRequired. That means that the user can upload any dimension of image.

Kindly help me get some ideas to make this functions flexible and allow the user to uploas a file without specifying the required measurements. Thanks




You could set some default parameters for$imageWidthRequired and$imageHeightRequired (like NULL) in the function definition and check if the values provided are NULL or something else before proceeding.


function addImage($imageWidthRequired = null, $imageHeightRequired = null) {
    if ($imageWidthRequired == null && $imageHeightRequired == null) {
        // image dimensions are not provided
    } else {
        // image dimensions are provided.

People are also looking for solutions to the problem: php - Swapping array values with Eloquent model IDs


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.