Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1033143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:11:16+00:00 2026-05-16T14:11:16+00:00

I have designed a file upload form in PHP where it takes 3 pictures

  • 0

I have designed a file upload form in PHP where it takes 3 pictures and stores it as a varchar datatype in database and move it to the destination directory.

before uploading or inserting the values to the database i want to make sure that

a) it is of file type image/jpeg

b) the three images should have a different fixed dimensions values (which i am fetching the dimension values from getimagesize(); and validating it)

c) and to check if the duplicate of file name exist in the directory (which i am checking through file_exists())

if any one of the conditions returns false then the script should die()

I have defined the code for it but the problem with my code is, if the last condition returns false then the upper conditions will execute the code and do some operations like move_uploaded_file which i dont want to happen, please have a look at the code

Please check the last 4 if conditions as i want that to reformat.

$w_title = 685;
    $h_title = 50;
    $w_brief = 685;
    $h_brief = 177;
    $w_detail = 685;

        if(empty($_POST['ns_title']) || empty($_FILES["ns_pic_title"]["name"]) || empty($_FILES["ns_pic_brief"]["name"]) || empty($_FILES["ns_pic_detail"]["name"])) {
           echo "<script type=\"text/javascript\">" . "alert(\"Please Fill All the Required Fields\");" . "</script>"; 
           echo "<meta http-equiv=\"refresh\" content=\"0;post-news.php\"/>";
           die();
        }
        else {
            $ns_title = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['ns_title'])));
            if($_FILES["ns_pic_title"]["type"] == "image/jpeg" && $_FILES["ns_pic_brief"]["type"] == "image/jpeg" && $_FILES["ns_pic_detail"]["type"] == "image/jpeg") {

                $ns_pic_title_loc= $_FILES["ns_pic_title"]["tmp_name"];
                $ns_pic_title_name = $_FILES["ns_pic_title"]["name"];
                list($width_title, $height_title) = getimagesize($ns_pic_title_loc);

                $ns_pic_brief_loc = $_FILES["ns_pic_brief"]["tmp_name"];
                $ns_pic_brief_name = $_FILES["ns_pic_brief"]["name"];
                list($width_brief, $height_brief) = getimagesize($ns_pic_brief_loc);

                $ns_pic_detail_loc = $_FILES["ns_pic_detail"]["tmp_name"];
                $ns_pic_detail_name = $_FILES["ns_pic_detail"]["name"];
                list($width_detail, $height_detail) = getimagesize($ns_pic_detail_loc);

                if(file_exists($ns_target.$ns_pic_title_name)) {
                    echo "<script type=\"text/javascript\">" . "alert(\"File Already Exists, Please Choose a Different Name for the File\");" . "</script>";
                    echo "<meta http-equiv=\"refresh\" content=\"0;post-news.php\"/>";
                    die();
                }

                if(!$width_title == $w_title && !$height_title == $h_title) {
                    echo "<script type=\"text/javascript\">" . "alert(\"Incorrect File Dimension for Title News, please make sure it is (685 X 50)\");" . "</script>";
                    echo "<meta http-equiv=\"refresh\" content=\"0;post-news.php\"/>";
                    die();
                }
                else {
                    move_uploaded_file($ns_pic_title_loc, $ns_target.$ns_pic_title_name);
                }

                if(!$width_brief == $w_brief && !$height_brief == $h_brief) {
                    echo "<script type=\"text/javascript\">" . "alert(\"Incorrect File Dimension for Brief News, please make sure it is (685 X 177)\");" . "</script>";
                    echo "<meta http-equiv=\"refresh\" content=\"0;post-news.php\"/>";
                    die();
                }
                else {
                    move_uploaded_file($ns_pic_brief_loc, $ns_target.$ns_pic_brief_name);
                }
                if(!$width_detail == $w_detail) {
                    echo "<script type=\"text/javascript\">" . "alert(\"Incorrect File Dimension for Detail News, please make sure it is (685 in width)\");" . "</script>";
                    echo "<meta http-equiv=\"refresh\" content=\"0;post-news.php\"/>";
                    die();
                }
                else {
                    move_uploaded_file($ns_pic_brief_loc, $ns_target.$ns_pic_brief_name);
                }

how do i reformat the code so that

a) it should check all the three condition

b) and if any one of it returns false then it should stop the execution at once

thank you

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:11:16+00:00Added an answer on May 16, 2026 at 2:11 pm

    I would recommend moving your logic to a function to reduce duplication.

    Note: I have no way to test any of this, so won’t try, but it should get you started.

    function fail($error)
    {
      echo '<script type="text/javascript">alert("' . $error . '");</script>';
      echo '<meta http-equiv="refresh" content="0;post-news.php"/>';
    }
    
    function valid_image($image, $width, $height = 0)
    {
      if ($image['type'] != 'image/jpeg')
      {
        fail('File must be of type image/jpeg');
        return false;
      }
    
      if(file_exists($ns_target . $image['name']))
      {
        fail('File Already Exists, Please Choose a Different Name for the File');
        return false;
      }
    
      list($image_width, $image_height) = getimagesize($image['tmp_name']);
      if ($image_width != $width || ($image_height && $image_height != $height))
      {
        fail('Incorrect File Dimension for ' . $image['name'] .
          ', please make sure it is (' . $width .
          ($height ? ' X ' . $height  : ' in width'). ')');
        return false;
      }
      return true;
    }
    
    if(empty($_POST['ns_title']) ||
      empty($_FILES["ns_pic_title"]["name"]) ||
      empty($_FILES["ns_pic_brief"]["name"]) ||
      empty($_FILES["ns_pic_detail"]["name"]))
    {
      fail('Please Fill All the Required Fields');
      die();
    }
    
    if (valid_image($_FILES['ns_pic_title'], 685, 50) &&
      valid_image($_FILES['ns_pic_brief'], 685, 177) &&
      valid_image($_FILES['ns_pic_detail'], 685))
    {
      move_uploaded_file($_FILES['ns_pic_title']['tmp_name'],
        $ns_target . $_FILES['ns_pic_title']['name']);
    
      move_uploaded_file($_FILES['ns_pic_brief']['tmp_name'],
        $ns_target . $_FILES['ns_pic_brief']['name']);
    
      move_uploaded_file($_FILES['ns_pic_detail']['tmp_name'],
        $ns_target . $_FILES['ns_pic_detail']['name']);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have designed a database whose MDF file will be copied to remote offices,
I have designed one sign-up form,in this form after getting all necessary values I
I have designed a button in my nib file. Its initial text is defined
I have designed my GUI form in the IntelliJ IDEA GUI designer, and selected
I have designed an xml file contain nine images i.e.., 3 rows each row
I have a form where a user selects a file, and then should be
I have designed a file copy software in python with GUI in Wxpython .
I have designed a custom UITableViewCell in the same nib file as my UITableView.
I have designed a form which has code behind attached to IT. I have
I have a product designed to be a desktop product using MS Access file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.