1

i have written a script to upload images,there are 2 input file upload to upload.. i have done validation in php to check which input is choosed or not so that table is not updated with blank if no image is choose.

Problem is that it update table if i choose both image..but not when 1 is choosed.

Code:

if ($_FILES["path"]["size"] > 0 && $_FILES["path1"]["size"] < 0) {
    $allowedExts = array("jpg", "jpeg", "png");
    $extension = end(explode(".", $_FILES["path"]["name"]));
//echo $extension;
    if (($extension == "jpeg")
        || ($extension == "jpg")
        || ($extension == "png")
    ) {
        if ($_FILES["path"]["error"] > 0) {
            $msg = $_FILES["path"]["error"] . "<br />";
        } else {
            move_uploaded_file($_FILES["path"]["tmp_name"],
                "../images/" . $_FILES["path"]["name"]);
        }

        $filename = "images/" . $_FILES["path"]["name"];

///update query
    }
} else if ($_FILES["path"]["size"] < 0 && $_FILES["path1"]["size"] > 0) {
    $allowedExts = array("jpg", "jpeg", "png");
    $extension = end(explode(".", $_FILES["path1"]["name"]));
//echo $extension;
    if (($extension == "jpeg")
        || ($extension == "jpg")
        || ($extension == "png")
    ) {
        if ($_FILES["path1"]["error"] > 0) {
            $msg = $_FILES["path1"]["error"] . "<br />";
        } else {
            move_uploaded_file($_FILES["path1"]["tmp_name"],
                "../images/" . $_FILES["path1"]["name"]);
        }

        $filename1 = "images/" . $_FILES["path1"]["name"];

///update query
    }
} else if ($_FILES["path"]["size"] < 0 && $_FILES["path1"]["size"] < 0) {
} else if ($_FILES["path"]["size"] > 0 && $_FILES["path1"]["size"] > 0) {
    $allowedExts = array("jpg", "jpeg", "png");
    $extension = end(explode(".", $_FILES["path"]["name"]));
//echo $extension;
    if (($extension == "jpeg")
        || ($extension == "jpg")
        || ($extension == "png")
    ) {
        if ($_FILES["path"]["error"] > 0) {
            $msg = $_FILES["path"]["error"] . "<br />";
        } else {
            move_uploaded_file($_FILES["path"]["tmp_name"],
                "../images/" . $_FILES["path"]["name"]);
        }

        $filename = "images/" . $_FILES["path"]["name"];

        $allowedExtss = array("jpg", "jpeg", "png");
        $extensions = end(explode(".", $_FILES["path1"]["name"]));
//echo $extension;
        if (($extensions == "jpeg")
            || ($extensions == "jpg")
            || ($extensions == "png")
        ) {
            if ($_FILES["path1"]["error"] > 0) {
                $msgs = $_FILES["path1"]["error"] . "<br />";
            } else {
                move_uploaded_file($_FILES["path1"]["tmp_name"],
                    "../images/" . $_FILES["path1"]["name"]);
            }

            $filename1 = "images/" . $_FILES["path1"]["name"];
///update query

        }
        header("Location: index.php?p=setings");
        exit;

I have checked each and every thing but no error found.

2
  • 1
    is the image get uploaded to the folder ? what is the update query? what is the status of php display_errors? Commented Nov 24, 2013 at 5:58
  • no.its not uploaded to folder...no error is there.. Commented Nov 24, 2013 at 6:01

1 Answer 1

1

all the conditions with $_FILES["path1"]["size"] < 0 is wrong. because it can be 0 or null or false but not lesser than 0

You need to change all of your conditions.

if ($_FILES["path"]["tmp_name"] != '' && $_FILES["path1"]["tmp_name"] == '') {

} else if ($_FILES["path"]["tmp_name"] == '' && $_FILES["path1"]["tmp_name"] != '') {

} else if ($_FILES["path"]["tmp_name"] == '' && $_FILES["path1"]["tmp_name"] == '') {

} else if ($_FILES["path"]["tmp_name"] != '' && $_FILES["path1"]["tmp_name"] != '') {

}

Also this program has a lot of redundant code.. consider the code below

if ($_FILES["path"]["tmp_name"] != '' || $_FILES["path1"]["tmp_name"] != '') {

    $allowedExts = array("jpg", "jpeg", "png");
    if ( $_FILES["path"]["tmp_name"] != '' ) {
        $extension = end(explode(".", $_FILES["path"]["name"]));

        if (($extension == "jpeg")
            || ($extension == "jpg")
            || ($extension == "png")
        ) {
            if ($_FILES["path"]["error"] > 0) {
                $msg = $_FILES["path"]["error"] . "<br />";
            } else {
                move_uploaded_file($_FILES["path"]["tmp_name"],
                    "../images/" . $_FILES["path"]["name"]);
            }

            $filename = "images/" . $_FILES["path"]["name"];
        }
    }

    if ( $_FILES["path1"]["tmp_name"] != '' ) { 
        $extensions = end(explode(".", $_FILES["path1"]["name"]));

        if (($extensions == "jpeg")
            || ($extensions == "jpg")
            || ($extensions == "png")
        ) {
            if ($_FILES["path1"]["error"] > 0) {
                $msgs = $_FILES["path1"]["error"] . "<br />";
            } else {
                move_uploaded_file($_FILES["path1"]["tmp_name"],
                    "../images/" . $_FILES["path1"]["name"]);
            }

            $filename1 = "images/" . $_FILES["path1"]["name"];
        }
    }

    header("Location: index.php?p=setings");
    exit;
} else {
    // no file
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks..much it works also ...it also worked with changing <0 to ==0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.