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

  • SEARCH
  • Home
  • 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 8741855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:23:26+00:00 2026-06-13T11:23:26+00:00

Possible Duplicate: Getting ‘undefined index’ error while trying to use $_FILE in PHP I

  • 0

Possible Duplicate:
Getting ‘undefined index’ error while trying to use $_FILE in PHP

I have made a php upload form for images. It uses session variables to determine the upload directory. There are two options for the uploads, the slider dir or the side dir so I have an if statement that determines the directory. If i remove this from the form then the whole thing works ok however with it in, $_FILES seems to not be declared and returns as an undefined index error.

The code can be found is as follows:

upload.php

<?php
include("resize-class.php");
$allowedExt = array('jpg', 'jpeg', 'JPG', 'JPEG');
$tmps = explode(".", $_FILES['file']['name']);
$extension = end($tmps);
session_start();
if ($_POST['dir'] == 'side'){
    $dirent = $_SESSION['sideDir'];
}
else if($_POST['dir'] == 'slider'){
    $dirent = $_SESSION['sliderDIR'];
}
else{
    die();
}
echo $_POST['dir'];
print_r($_FILES);

if (($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 4000000000)&& in_array($extension, $allowedExt)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
        echo 'here';
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . " <br />\n";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> \n";
        echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br />\n";

    }
    if (file_exists($dirent. $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . "already exists";
    } else {
        $fName = $_FILES["file"]["name"];
        $tmpname = $_FILES["file"]["tmp_name"];
        move_uploaded_file($_FILES["file"]["tmp_name"], $dirent . $_FILES["file"]["name"]);
        $number = FileCounter($dirent);
        echo "Number of images in DIR: " . $number. "   <br />\n  ";
        $number +1;
        $resizeObj = new resize($dirent.$fName);
        $resizeObj -> resizeImage(250, 150, 'crop');  
        $resizeObj -> saveImage($dirent.$number.".jpg", 100); 
        unlink ($dirent.$_FILES["file"]["name"]);
    }
} else {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
function FileCounter($dir) {
            $counter = 0;
            $iterator = new DirectoryIterator(dirname($dir));
            foreach ($iterator as $fileinfo) {
                if ($fileinfo->isFile()) {
                    if ($fileinfo->getExtension() == "jpg") {
                        $counter++;
                        echo $counter . "\n";
                    }
                }
            }
            return $counter;
        }
?>

HTML FORM:

<form action="includes/upload.php" method="post">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <div class="styled-select">
    <label for="dir"> Upload to:</label>
    <select size="2" name="dir" multiple="yes" id="dir">
      <option value="side" >Side Images</option>
      <option value="slider" >Slider Images</option>
    </select>
  </div>
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>

I am guessing that there is either a really stupid error in my code that I am overlooking as I have been staring at it for an hour now or that there is something that I do not know about $_FILES and $_POST. (or possibly I have coded the form like a moron!).

  • 1 1 Answer
  • 0 Views
  • 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-06-13T11:23:27+00:00Added an answer on June 13, 2026 at 11:23 am

    Your form is missing

    enctype="multipart/form-data"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index i am getting ranksection
Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” I am getting warnings.
Possible Duplicate: Why am I getting Undefined index from my PHP? $pattern2 = /([A-Za-z0-9\.\-\_\!\#\$\%\&\'\*\+\/\=\?\^\`\{\|\}]+)\@([A-Za-z0-9.-_]+)(\.[A-Za-z]{2,5})/;
Possible Duplicate: Parsing errors in awk blocks I am getting a parsing error while
Possible Duplicate: Undefined index: file Hi I'm learning right now how to upload images
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I am just learning
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index Okay, so I am
Possible Duplicate: Undefined reference - C++ linker error Now, I'm getting an Undefined reference
Possible Duplicate: Getting text within element excluding decendants I'm trying to use :not and
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index You all are probably

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.