[SOLVED SOLUTION] Why this error "Notice: Undefined index: "

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lindylex
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 1:26 pm

[SOLVED SOLUTION] Why this error "Notice: Undefined index: "

Post by lindylex »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I am trying to upload some files using php and I am getting inconsistent errors. Everything works if I upload one or two files. When I try to upload three file the code breaks, but not all the time. 1/30 plus tries it will work. I have no idea what is going on.

This is forum/Html

Code: Select all

 
 
<html>
<?php
function display_themes_in_drop_down($path ){
//Strip The Underscore Out
$things_to_replace="/_/";
$replace_with=' ';
 
//  Open  the  folder 
$dir_handle  =  opendir($path)  or  die("Unable  to  open  $path"); 
//  Loop  through  the  files 
    while  ($file  =  readdir($dir_handle))  { 
        if ($file !='..' && $file !='.'){
            //echo  "$file<br>"; 
            $file= preg_replace ($things_to_replace, $replace_with ,$file);
            echo '<option>'.$file.'</option>';
        }
    } 
//  Close 
closedir($dir_handle);
}
?>
 
<HEAD>
<SCRIPT language="JavaScript" SRC="../js/form_validation.js"></SCRIPT>
</HEAD> 
 
<title>:::Upload Your Image:::</title>
<body>
 
 
<!--<form enctype="multipart/form-data" name="stuff_to_upload" action="" onsubmit="return check_if_theme_exist (this);" method="post">-->
<form enctype="multipart/form-data" name="stuff_to_upload" action="create_del_folders.php" onsubmit="return check_if_theme_exist (this);" method="post">
<!--<form name="stuff_to_upload" onsubmit="return check_if_theme_exist (this);" method="post">-->
 
Choose a topic to upload to &nbsp;&nbsp;&nbsp; Or &nbsp;&nbsp;&nbsp;Create a topic to upload to<br> <br>
 
    <select name="preview_themes" onChange = "limit_one_theme_only(this)">
        <option>Choose a theme</option>
        <?php display_themes_in_drop_down ("../photos");?>
    </select>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" size = "20" name="create_this_folder" onKeyPress = "limit_one_theme_only_1(this)"/> <br><br>
 
Choose files to upload:
<br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="10240000" />
 
Downloadable<input type="checkbox" name="dl[]">
    <select name="experation[]">
        <!--<option>-------------------</option>-->
        <option>One Month</option>
        <option>Two Months</option>
        <option>Forever</option>
    </select>
 
 <input name="uploadedfile[]" type="file" /> 
 
<!--  Signature Photo<input type="checkbox" name="signature0" value="true"> -->
 Color <input type="radio" name="borc0" value="c" >
 B/W <input type="radio" name="borc0" value="b">
 
 
 <br>
 
 Downloadable<input type="checkbox" name="dl[]">
    <select name="experation[]">
        <!--<option>-------------------</option>-->
        <option>One Month</option>
        <option>Two Months</option>
        <option>Never</option>
    </select>
 
 <input name="uploadedfile[]" type="file" />
 
<!-- Signature Photo<input type="checkbox" name="signature1" value="true"> -->
 Color <input type="radio" name="borc1" value="c" >
 B/W <input type="radio" name="borc1" value="b">
 
 <br>
 
  Downloadable<input type="checkbox" name="dl[]">
    <select name="experation[]">
        <!--<option>-------------------</option>-->
        <option>One Month</option>
        <option>Two Months</option>
        <option>Never</option>
    </select>
 
 <input name="uploadedfile[]" type="file"/>
 
 <!-- Signature Photo<input type="checkbox" name="signature2" value="true"> -->
 Color <input type="radio" name="borc2" value="c" >
 B/W <input type="radio" name="borc2" value="b">
 
<br><br>
<input type = "submit" value="Upload File"/>
</form>
 
</body>
</html>
 
 
 
This is my code that accesses the information sent from the forum.

Code: Select all

 
 
<?php
 
var_dump($_FILES, $_POST);
//var_dump($_FILES);
 
echo "<br><br>---------------------------------------------------- <br><br>";
 
 
 
$things_to_replace="/\s/";
$replace_with='_';
 
$path_of_folder=preg_replace ($things_to_replace, $replace_with ,$_POST["create_this_folder"]);
 
//Cleanup work because of IE 6
if (strlen($path_of_folder)!=0){
    if ($path_of_folder[0]=='_'){
        $path_of_folder=substr ($path_of_folder,1);
    }
}
 
if (strlen($_POST["create_this_folder"]) == 0){
        echo strlen($_POST["create_this_folder"])." << the length of form field create_this_folder <br>";
        echo "lllloookkk <br>";
        
        for ($i=0; $i<3; $i++){
 
                echo $_FILES ['uploadedfile']['tmp_name'][$i]." << temp name <br>";
                echo $_FILES ['uploadedfile']['name'][$i]." << original name <br>";
                echo $_FILES ['uploadedfile']['type'][$i]." << type <br>";
                echo $_FILES ['uploadedfile']['size'][$i]." << size<br>";
            
            
        }
}
 
?>
 
 

This the output of var_dump($_FILES, $_POST); when I upload two files.

Code: Select all

 
 
array(1) { ["uploadedfile"]=>  array(5) { ["name"]=>  array(3) { [0]=>  string(12) "DSC_0500.JPG" [1]=>  string(12) "DSC_0624.JPG" [2]=>  string(0) "" } ["type"]=>  array(3) { [0]=>  string(10) "image/jpeg" [1]=>  string(10) "image/jpeg" [2]=>  string(0) "" } ["tmp_name"]=>  array(3) { [0]=>  string(80) "C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\phpDB.tmp" [1]=>  string(80) "C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\phpDC.tmp" [2]=>  string(0) "" } ["error"]=>  array(3) { [0]=>  int(0) [1]=>  int(0) [2]=>  int(4) } ["size"]=>  array(3) { [0]=>  int(2376919) [1]=>  int(3448569) [2]=>  int(0) } } } array(4) { ["preview_themes"]=>  string(3) "dsl" ["create_this_folder"]=>  string(0) "" ["MAX_FILE_SIZE"]=>  string(8) "10240000" ["experation"]=>  array(3) { [0]=>  string(9) "One Month" [1]=>  string(9) "One Month" [2]=>  string(9) "One Month" } }
 
----------------------------------------------------
 
0 << the length of form field create_this_folder
lllloookkk
C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\phpDB.tmp << temp name
DSC_0500.JPG << original name
image/jpeg << type
2376919 << size
C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\phpDC.tmp << temp name
DSC_0624.JPG << original name
image/jpeg << type
3448569 << size
<< temp name
<< original name
<< type
0 << size
 
 
 
This is the error I get when I upload three files.

Code: Select all

 
 
array(0) { } array(0) { }
 
----------------------------------------------------
 
 
Notice: Undefined index: create_this_folder in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 13
 
Notice: Undefined index: create_this_folder in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 22
 
Notice: Undefined index: create_this_folder in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 23
0 << the length of form field create_this_folder
lllloookkk
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 28
<< temp name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 29
<< original name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 30
<< type
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 31
<< size
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 28
<< temp name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 29
<< original name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 30
<< type
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 31
<< size
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 28
<< temp name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 29
<< original name
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 30
<< type
 
Notice: Undefined index: uploadedfile in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php_study\manipulate_image\php_code\create_del_folders.php on line 31
<< size
 
 
 
Does anyone have ideas?

Thanks, Lindylex


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Last edited by lindylex on Fri Apr 11, 2008 6:40 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Why this error "Notice: Undefined index: "

Post by pickle »

An undefined index error means you're trying to access an array element that hasn't been declared.

Code: Select all

//This will generate that error
$myArray = array();
 
echo $myArray['myKey'];
 
// This won't
$myArray = array();
$myArray['myKey'] = 'myValue';
 
echo $myArray['myKey'];
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lindylex
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 1:26 pm

Re: Why this error "Notice: Undefined index: "

Post by lindylex »

pickle, the array comes from form <input name="uploadedfile[]" type="file" />

These all contain a file I selected. I am not sure how it could be empty. As you can see when I select two files to upload I have no problems.

This Notice: Undefined index: create_this_folder also breaks when I try to upload three files but as you can see it has a value when I try to upload two or less files.

So I have no idea what index I am trying to reference that does not contain a value. Why would it have a value when I upload two or one files but breaks when I upload three?

Lex
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Why this error "Notice: Undefined index: "

Post by pickle »

I'm not sure how you can upload multiple files with that form - you've only got 1 <input type = "file"> element as far as I can see, which should only allow you to upload 1 file at a time - how are you uploading 2?

Also, if you put <pre></pre> tags around your var_dump(), it will be formatted MUCH nicer & easier to read.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lindylex
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 1:26 pm

Re: Why this error "Notice: Undefined index: "

Post by lindylex »

pickle, look for this line.
"<input name="uploadedfile[]" type="file" />"
it occurs three times.

uploadedfile[] this is going to be an array in php. you can access the content of this array like this.

Code: Select all

 
 
        for ($i=0; $i<3; $i++){
                echo $i." << counting var i <br>";
                echo $_FILES ['uploadedfile']['tmp_name'][$i]." << temp name <br>";
                echo $_FILES ['uploadedfile']['name'][$i]." << original name <br>";
                echo $_FILES ['uploadedfile']['type'][$i]." << type <br>";
                echo $_FILES ['uploadedfile']['size'][$i]." << size<br>";
            
        }
 
 
Your right thanks the output looks much better I had no idea. This is the code working with two files selected for upload but three will break it.

Code: Select all

array(1) {
  ["uploadedfile"]=>
  array(5) {
    ["name"]=>
    array(3) {
      [0]=>
      string(12) "DSC_0633.JPG"
      [1]=>
      string(12) "DSC_0618.JPG"
      [2]=>
      string(0) ""
    }
    ["type"]=>
    array(3) {
      [0]=>
      string(10) "image/jpeg"
      [1]=>
      string(10) "image/jpeg"
      [2]=>
      string(0) ""
    }
    ["tmp_name"]=>
    array(3) {
      [0]=>
      string(81) "C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\php197.tmp"
      [1]=>
      string(81) "C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\php198.tmp"
      [2]=>
      string(0) ""
    }
    ["error"]=>
    array(3) {
      [0]=>
      int(0)
      [1]=>
      int(0)
      [2]=>
      int(4)
    }
    ["size"]=>
    array(3) {
      [0]=>
      int(3113621)
      [1]=>
      int(3337526)
      [2]=>
      int(0)
    }
  }
}
array(4) {
  ["preview_themes"]=>
  string(3) "dsl"
  ["create_this_folder"]=>
  string(0) ""
  ["MAX_FILE_SIZE"]=>
  string(8) "10240000"
  ["experation"]=>
  array(3) {
    [0]=>
    string(9) "One Month"
    [1]=>
    string(9) "One Month"
    [2]=>
    string(9) "One Month"
  }
}
 
 
 
----------------------------------------------------
 
0 << the length of form field create_this_folder
lllloookkk
0 << counting var i
C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\php197.tmp << temp name
DSC_0633.JPG << original name
image/jpeg << type
3113621 << size
1 << counting var i
C:\Documents and Settings\Administrator\Local Settings\Temp\PHP\upload\php198.tmp << temp name
DSC_0618.JPG << original name
image/jpeg << type
3337526 << size
2 << counting var i
<< temp name
<< original name
<< type
0 << size
 
 
dsl <<< testing index
Last edited by lindylex on Fri Apr 11, 2008 5:47 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Why this error "Notice: Undefined index: "

Post by pickle »

Ah ok - guess I should read eh?

Well my next step would be to simplify everything. Make a simple form with just 3 file upload fields & a submit button. Get that working. Then, one by one, add your other fields.

And please wrap your code in tags ike I mentioned & fixed in your first post - it makes code much easier to read.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lindylex
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 1:26 pm

Re: Why this error "Notice: Undefined index: "

Post by lindylex »

[SOLVED SOLUTION]

Edit the PHP configuration file it should be "php.ini".

Find the following line.

; Maximum size of POST data that PHP will accept.
post_max_size = 8M

Chang it to an acceptable size that should match the max upload worst case scenario.

post_max_size = 20M

You're done, this only took 4 days resolve. Also get XDEBUG http://www.xdebug.org/docs/install

This will help you debug your work much better than what php has built in, check Apache logs also.

Thanks, Lindylex
Post Reply