Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Check to make sure image is either dimensions?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #242545
    cscodismith
    Participant

    I have a function on my website that is not yet released that admins will use to make posts on the homepage. Part of this function involves uploading an image and there is currently no check for this but would like to make it so that it will check to make sure that they are only uploading images that are either one of the following resolutions:

    1920px width;1080px height
    1280px width;720px height
    

    I am not to sure how to check this but my current code for uploading images looks like the following:

    <?php
    function uploadImage() {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
        }
        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
            echo "Sorry, only JPG, JPEG, & PNG files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                return $target_file;
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    
    #242557
    Beverleyh
    Participant

    Untested but it might work if you change

    if($check !== false) {
    

    to

    if ( ($check !== false) && ($check[0] == 1920 && $check[1] == 1080) || ($check[0] == 1280 && $check[1] == 720) ) {
    
    #242558
    I.m.learning
    Participant

    Take a look at this page
    http://codular.com/php-file-uploads

    #242586
    cscodismith
    Participant

    Unfortunately this doesn’t check it the way I would like. It still allows any size image to be uploaded :(

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.