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 5937127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:29:43+00:00 2026-05-22T15:29:43+00:00

I am getting the undefined index error as I come for the first time

  • 0

I am getting the undefined index error as I come for the first time in my upload form page or if I move to next page and click on back button then I have the same error message.
If I upload a file then it works fine and the error message gets away.

I have also tried this:

global $file;
if (!isset($file)) {
    $file = '';
}

Here is my code:

<form id="uploadForm" name="upload" enctype="multipart/form-data"/>
  <fieldset>
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input type="file" name="file" />
<?php
echo '<pre>';
var_dump($_REQUEST['file']);
echo '</pre>';

$uploaded = new upload;
//set Max Size
$uploaded->set_max_size(350000);
//Set Directory
$uploaded->set_directory("data");
//Set Temp Name for upload.
$uploaded->set_tmp_name($_FILES['file']['tmp_name']);
//Set file size
$uploaded->set_file_size($_FILES['file']['size']);
//set file type
$uploaded->set_file_type($_FILES['file']['type']);
//set file name
$uploaded->set_file_name($_FILES['file']['name']);
//start copy process
$uploaded->start_copy();
if($uploaded->is_ok()) 
    echo " upload is doen.";
else
    $uploaded->error()."<br>";
?>
    <div class="filesize">JPG minimaal 800x60 pixels max. 2Mb</div>
    <a href="" class="submit" title="Upload your own phooto"><span> Upload your own photo </span></a>

upload_inc.php

<?
class upload
{
    var $directory_name;
    var $max_filesize;
    var $error;

    var $user_tmp_name;
    var $user_file_name;
    var $user_file_size;
    var $user_file_type;
    var $user_full_name;

    function set_directory($dir_name =".")
    {
        $this->directory_name = $dir_name;
    }

    function set_max_size($max_file = 2000000)
    {
        $this->max_filesize = $max_file;
    }

    function error()
    {
        return $this->error;
    }

    function is_ok()
    {
        if(isset($this->error))
            return FALSE;
        else
            return TRUE;
    }

    function set_tmp_name($temp_name)
    {
        $this->user_tmp_name = $temp_name;
    }

    function set_file_size($file_size)
    {
         $this->user_file_size = $file_size;
    }

    function set_file_type($file_type)
    {
        $this->user_file_type = $file_type;
    }

    function set_file_name($file)
    {
        $this->user_file_name = $file;
        $this->user_full_name = $this->directory_name."/".$this->user_file_name;
    }

    function start_copy()
    {
        if(!isset($this->user_file_name))
            $this->error = "You must define filename!";
        if ($this->user_file_size <= 0)
            $this->error = 'File size error (0):' . $this->user_file_size . 'KB <br>';
        if ($this->user_file_size > $this->max_filesize)
            $this->error = 'File size error (1):' . $this->user_file_size . 'KB<br>';
        if($this->user_file_type != "image/jpeg")
            $this->error = "the image must be jpeg extension";
        if (!isset($this->error))
        {
            $filename = basename($this->user_file_name);
            if (!empty($this->directory_name))
                $destination = $this->user_full_name;
            else
                $destination = $filename;
            if(!is_uploaded_file($this->user_tmp_name))
                $this->error = "File " . $this->user_tmp_name . " is not uploaded correctly.";
            if (!move_uploaded_file ($this->user_tmp_name,$destination))
                $this->error = "Impossible to copy " . $this->user_file_name . " from your folder to destination directory.";
        }
    }

}
?>
  • 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-05-22T15:29:44+00:00Added an answer on May 22, 2026 at 3:29 pm

    "Undefined index" means you’re trying to read an array element that doesn’t exist.

    Your specific problem seems to be that you’re trying to read upload data that doesn’t exist yet: When you first visit your upload form, there is no $_FILES array (or rather, there’s nothing in it), because the form hasn’t been submitted. But since you don’t check if the form was submitted, these lines net you an error:

    //Set Temp Name for upload.
    $uploaded->set_tmp_name($_FILES['file']['tmp_name']);
    //Set file size
    $uploaded->set_file_size($_FILES['file']['size']);
    //set file type
    $uploaded->set_file_type($_FILES['file']['type']);
    //set file name
    $uploaded->set_file_name($_FILES['file']['name']);
    

    They’re all trying to read the value of $_FILES['file'] to pass them to the methods of $uploaded.

    What you need is a check beforehand:

    if (isset($_FILES['file'])) {
        $uploaded = new upload;
        //set Max Size
        $uploaded->set_max_size(350000);
        //Set Directory
        $uploaded->set_directory("data");
        //Set Temp Name for upload.
        $uploaded->set_tmp_name($_FILES['file']['tmp_name']);
        //Set file size
        $uploaded->set_file_size($_FILES['file']['size']);
        //set file type
        $uploaded->set_file_type($_FILES['file']['type']);
        //set file name
        $uploaded->set_file_name($_FILES['file']['name']);
        //start copy process
        $uploaded->start_copy();
        if($uploaded->is_ok()) 
            echo " upload is doen.";
        else
            $uploaded->error()."<br>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting Undefined index: filename error in the below image upload php code. Is there
I am getting an Undefined index error when submitting a form with an un-checked
I am getting an undefined index on my config page and it reads: Notice
I keep getting an Notice: Undefined index: did error with this query, and I'm
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})/;
I'm getting this error: undefined method `post_comments_path' for #<#<Class:0x1052a6e98>:0x1052a4be8> Extracted source (around line #27):
I am getting an error: Undefined method build for nil:NilClass when trying to build
i am getting undefined property error on my model. I m using matchbox library
I am getting NoMethodError in SitesController#index undefined method `subdomain' for nil:NilClass I have an
I'm getting this strange error using Rails 3.0.2. ActionView::Template::Error (undefined method `parent' for nil:NilClass):

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.