Notice: Undefined index Error. Help!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talk2tim
    New Member
    • Feb 2015
    • 19

    Notice: Undefined index Error. Help!!!

    Hello Folks. Thanks for all the help i have received here. I was trying to write an upload file. It was ok until i began error reporting. First i tried to inspect the file uploaded by asking the print_r to show the associate array of name, type, size, tmp_name and error. It works all right but it gives this error just as i load the page


    Notice: Undefined index: file_upload in C:\wamp\www\btb _sandbox\upload .php on line 27



    I didnt take it quite serious then i continues my error reporting. It gave the following errors


    Notice: Undefined index: file_upload in C:\wamp\www\btb _sandbox\upload .php on line 18

    Notice: Undefined index: in C:\wamp\www\btb _sandbox\upload .php on line 19


    I understand that file_upload is undefined, but i dont know how to go around it. Or is it from my html tags? Please help out. Here is the code. Thanks


    Code:
    <?php
       
        //In an application, this could be moved to a config file
        $upload_errors = array
            (//http://www.php.net/manual/en/features.file-upload.errors.php
    
                    UPLOAD_ERR_OK           =>  "No Errors.", 
                    UPLOAD_ERR_INI_SIZE     =>  "Larger than upload_max_filesize.", 
                    UPLOAD_ERR_FORM_SIZE    =>  "Larger than form MAX_FILE_SIZE.", 
                    UPLOAD_ERR_PARTIAL      =>  "Partial upload.", 
                    UPLOAD_ERR_NO_FILE      =>  "No file.", 
                    UPLOAD_ERR_NO_TMP_DIR   =>  "No temporary directory", 
                    UPLOAD_ERR_CANT_WRITE   =>  "Can't write to disk",
                    UPLOAD_ERR_EXTENSION    =>  "File upload stopped by extension."
            );
            
    
            $error = $_FILES['file_upload']['error'];
            $message = $upload_errors[$error];
    
    
    
    
    
        //print_r( is the one giving me Notice:  Undefined index:  file_upload in C:\wamp\www\btb_sandbox\upload.php on line 4. Echo makes it go, nut show nothing because it aint redable by humans
        echo "<pre>";
        print_r($_FILES['file_upload']);
        echo "</pre>";
        echo "<hr/>";
    
        
    ?>
    
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title> Upload </title>
        </head>
        <body>
            
    
            <?php if(!empty($message)){echo "<p>{$message}</p>";} //A means to pass messages to the the user about the from processing?>
            <form action="upload.php" enctype="multipart/form-data" method="POST">
    
               <input type="hidden" name="MAX_FILE_SIZE" value="100000000"/> 
    
                <input type="file" name="file_upload"/>
    
                <input type="submit" value="Upload"/>
            
            </form>
    
                
              
    
            
        </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you call the page in order to upload a file, the $_FILES array is empty.

    Comment

    • talk2tim
      New Member
      • Feb 2015
      • 19

      #3
      Thanks Dormilich, so how can i solve it???

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you check if $_FILES is populated and only then run the upload code.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          FWD:
          Code:
          <?php 
              
           if(isset($_POST['file_upload'])) 
              {   
              $upload_errors = array 
                  ( 
                          UPLOAD_ERR_OK           =>  "No Errors.",  
                          UPLOAD_ERR_INI_SIZE     =>  "Larger than upload_max_filesize.",  
                          UPLOAD_ERR_FORM_SIZE    =>  "Larger than form MAX_FILE_SIZE.",  
                          UPLOAD_ERR_PARTIAL      =>  "Partial upload.",  
                          UPLOAD_ERR_NO_FILE      =>  "No file.",  
                          UPLOAD_ERR_NO_TMP_DIR   =>  "No temporary directory",  
                          UPLOAD_ERR_CANT_WRITE   =>  "Can't write to disk", 
                          UPLOAD_ERR_EXTENSION    =>  "File upload stopped by extension." 
                  ); 
                   
          
                  $error = $_FILES['file_upload']['error'];//Notice:  Undefined index:  file_upload 
                  $message = $upload_errors[$error]; 
          
            
              echo "<pre>"; 
              print_r($_FILES['file_upload']);//Notice:  Undefined index:  file_upload  
              echo "</pre>"; 
              echo "<hr/>"; 
              } 
               
          ?> 
          
          
          <!DOCTYPE html> 
          
          <html lang="en"> 
              <head> 
                  <meta charset="utf-8" /> 
                  <title> Upload </title> 
              </head> 
              <body> 
                 
          
                  <?php if(!empty($message)){echo "<p>{$message}</p>";}?> 
                  <form action="upload.php" enctype="multipart/form-data" method="POST"> 
          
                     <input type="hidden" name="MAX_FILE_SIZE" value="100000000"/>  
          
                      <input type="file" name="file_upload"/> 
          
                      <input type="submit" value="Upload"/> 
                   
                  </form> 
          
          
                   
              </body> 
          </html>
          It removed the error, but it doesnt seem to process the form or even output the associative array that i asked it for. It was just like the all php above <!DOCTYPE html> were removed. The page just opens with only CHOOSE FILE and UPLOAD buttons, and when i try to upload, it refuses, telling me "No file chosen". Same thing happens with if(isset($_POST['file_upload'])){....My php code here..}. Is it because the page is submitting to itself?Or the the whole form processing is on one page?? Thanks

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            your data is in $_FILES, not in $_POST.

            there’s a pretty good example here.

            Comment

            • talk2tim
              New Member
              • Feb 2015
              • 19

              #7
              Thanks Dormilich. You have really been helpful. i look at that php.net link that you showed to me and it was really helpful. Im pretty new to php. I spent all night working on this bug, yet no progress. Same error, or some other. Just now, i came up with an idea. If it was about the index 'file_upload' not being set,, or not being an array, why not a simple echo statement. Thus i took the initiative from the php.net site and did something like this

              Code:
                 if (
                      !isset($_FILES['file_upload']['error']) ||
                      is_array($_FILES['file_upload']['error'])
                         ) 
                  
                            {echo "File Upload is set";}
              It returned "File Upload is set", Thus i guess thats a step forward. Im stuck here. I have tried everything i could to weave syntax to accommodate the rest of the code yet, same stuff. Last stuff i tried was this

              Code:
              if (
                      !isset($_FILES['file_upload']['error']) ||
                      is_array($_FILES['file_upload']['error'])
                         ) 
                  
                            {  $upload_errors = array 
                                    ( 
                              UPLOAD_ERR_OK           =>  "No Errors.",  
                              UPLOAD_ERR_INI_SIZE     =>  "Larger than upload_max_filesize.",  
                              UPLOAD_ERR_FORM_SIZE    =>  "Larger than form MAX_FILE_SIZE.",  
                              UPLOAD_ERR_PARTIAL      =>  "Partial upload.",  
                              UPLOAD_ERR_NO_FILE      =>  "No file.",  
                              UPLOAD_ERR_NO_TMP_DIR   =>  "No temporary directory",  
                              UPLOAD_ERR_CANT_WRITE   =>  "Can't write to disk", 
                              UPLOAD_ERR_EXTENSION    =>  "File upload stopped by extension." 
                                   ); 
               
               
                      $error = $_FILES['file_upload']['error'];//Notice:  Undefined index:  file_upload 
                      $message = $upload_errors[$error]; 
               
               
                  echo "<pre>"; 
                  print_r($_FILES['file_upload']);//Notice:  Undefined index:  file_upload  
                  echo "</pre>"; 
                  echo "<hr/>"; 
                  } 
               
              ?> 
               
               
              <!DOCTYPE html> 
               
              <html lang="en"> 
                  <head> 
                      <meta charset="utf-8" /> 
                      <title> Upload </title> 
                  </head> 
                  <body> 
               
               
                      <?php if(!empty($message)){echo "<p>{$message}</p>";}?> 
                      <form action="upload.php" enctype="multipart/form-data" method="POST"> 
               
                         <input type="hidden" name="MAX_FILE_SIZE" value="100000000"/>  
               
                          <input type="file" name="file_upload"/> 
               
                          <input type="submit" value="Upload"/> 
               
                      </form> 
                  </body> 
              </html>

              Could you please help me weak the right syntax for this my code? Thanks a trillion :-)

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                well, your if condition triggers if:
                - $_FILES['file_upload']['error'] does not exist or
                - $_FILES['file_upload']['error'] is an array
                Last edited by Dormilich; Mar 3 '15, 01:51 PM.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  compare what is written in the comment vs. what is written in your code!

                  Comment

                  • talk2tim
                    New Member
                    • Feb 2015
                    • 19

                    #10
                    OHH! My bad. I think isset && is_array would have been better

                    Code:
                    <?php
                       
                    
                     $upload_errors = array
                            (
                                    UPLOAD_ERR_OK           =>  "No Errors.", 
                                    UPLOAD_ERR_INI_SIZE     =>  "Larger than upload_max_filesize.", 
                                    UPLOAD_ERR_FORM_SIZE    =>  "Larger than form MAX_FILE_SIZE.", 
                                    UPLOAD_ERR_PARTIAL      =>  "Partial upload.", 
                                    UPLOAD_ERR_NO_FILE      =>  "No file.", 
                                    UPLOAD_ERR_NO_TMP_DIR   =>  "No temporary directory", 
                                    UPLOAD_ERR_CANT_WRITE   =>  "Can't write to disk",
                                    UPLOAD_ERR_EXTENSION    =>  "File upload stopped by extension."
                            );
                            
                        if  (
                            isset($_FILES['file_upload']['error']) &&
                            is_array($_FILES['file_upload']['error'])
                            ) 
                        
                        {
                                echo "Defined and ready";
                        }
                    
                        else {echo "Undefined and Unready";}
                        
                    ?>
                    It returned Undefined and unready. So please, what should i do to define it and make it an array. Thanks in advance

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      why—do you think—should $_FILES['file_upload']['error'] be an array?

                      Comment

                      • talk2tim
                        New Member
                        • Feb 2015
                        • 19

                        #12
                        No. $upload_errors is the array. What should i do to at least define 'file_upload'? Am confused right now

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          $upload_errors is of no interest for the problem.

                          let’s have a look at the original snippet again

                          Code:
                          try {
                              
                              // Undefined | Multiple Files | $_FILES Corruption Attack
                              // If this request falls under any of them, treat it invalid.
                              if (
                                  !isset($_FILES['upfile']['error']) ||
                                  is_array($_FILES['upfile']['error'])
                              ) {
                                  throw new RuntimeException('Invalid parameters.');
                              }
                          
                              // Check $_FILES['upfile']['error'] value.
                              switch ($_FILES['upfile']['error']) {
                                  case UPLOAD_ERR_OK:
                                      break;
                                  case UPLOAD_ERR_NO_FILE:
                                      throw new RuntimeException('No file sent.');
                                  case UPLOAD_ERR_INI_SIZE:
                                  case UPLOAD_ERR_FORM_SIZE:
                                      throw new RuntimeException('Exceeded filesize limit.');
                                  default:
                                      throw new RuntimeException('Unknown errors.');
                              }
                          
                              // ...
                          line #5 - #10 is a preliminary check for the code block (#13 - #23) following it. the key to understanding the check is to understand what the variable $_FILES['upfile']['error'] is used for.

                          it is used in a switch() that tests it for a single value (an integer, to be precise)! hence we should make sure that $_FILES['upfile']['error'] 1) exists and 2) it contains a primitive value (string, integer, boolean).

                          we know that user data (via $_GET, $_POST, etc.) come in exactly 2 types: string and array. an array is not a primitive, so it needs to be excluded.

                          these two tests are made before the switch. and due to the use of exceptions the test is made in reverse: it tests if one of the required conditions (existence, data type) fails and if one fails the error handling kicks in. (this is one way to prevent nesting hell) If both requirements are met, i.e. both failure conditions fail, the script can proceed.

                          Comment

                          • talk2tim
                            New Member
                            • Feb 2015
                            • 19

                            #14
                            Thanks Dormilich. You have been far too kind. I have cracked it. An elder colleague advised me to run the isset test after the $upload_errors array and not before it. I removed those <pre> tags that i used in inspecting the uploaded files and later decided against running the test on 'file_upload'. Instead i checked whether the $_POST variable was subumitted. Like this

                            Code:
                            if(isset($_POST['submit']))
                                    {..my form processing code here..}
                            It worked. I thus proceeded to move the uploaded file to a more permanent directory with the original name(basename) and give a success message or error message utilizing that $upload_errors array. The problem was never 'file_uploads'. I have pasted the full code below.

                            Code:
                            
                            <?php
                                
                               
                            $upload_errors = array(
                            
                            	UPLOAD_ERR_OK 			=> "No errors.",
                            	UPLOAD_ERR_INI_SIZE  	=> "Larger than upload_max_filesize.",
                              UPLOAD_ERR_FORM_SIZE  	=> "Larger than form MAX_FILE_SIZE.",
                              UPLOAD_ERR_PARTIAL 		=> "Partial upload.",
                              UPLOAD_ERR_NO_FILE 		=> "No file.",
                              UPLOAD_ERR_NO_TMP_DIR     => "No temporary directory.",
                              UPLOAD_ERR_CANT_WRITE     => "Can't write to disk.",
                              UPLOAD_ERR_EXTENSION 	    => "File upload stopped by extension."
                            );
                            
                                if(isset($_POST['submit']))
                                    {
                            	
                            	$tmp_file = $_FILES['file_upload']['tmp_name'];
                            	$target_file = basename($_FILES['file_upload']['name']); 
                                                                                
                              
                            	$upload_dir = "uploads";
                            
                            
                            	    if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) 
                                        {
                            		        $message = "File uploaded successfully.";
                            	        } 
                                    else 
                                        {
                            		        $error = $_FILES['file_upload']['error'];
                            		        $message = $upload_errors[$error];
                            	        }
                            	
                                    }
                                        	
                            
                            ?>
                            <html>
                            	<head>
                            		<title>Upload</title>
                            	</head>
                            	<body>
                            	
                            
                            		<?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?>
                            		<form action="upload.php" enctype="multipart/form-data" method="POST">
                            
                            		  <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
                            		  <input type="file" name="file_upload" />
                            
                            		  <input type="submit" name="submit" value="Upload" />
                            		</form>
                            	
                            	</body>
                            </html>

                            Thanks alot Dormilich. I appreciate. I learnt alot during this debugging process. I hope this helps someone that has a similar pitfall in the future. Gracias.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              An elder colleague advised me to run the isset test after the $upload_errors array and not before it.
                              that array is just a convenience to translate the PHP constants into readable text. it serves no purpose for the actual error checking.

                              I […] later decided against running the test on 'file_upload'. Instead i checked whether the $_POST variable was subumitted.
                              which makes you highly prone to upload errors.

                              imagine the following scenario: what happens in your code if the form is submitted, but no file uploaded?

                              starting with line #21 you’ll get warnings (missing index) to errors (no such file) all over the place. and only after everything has failed, you check what was actually the problem.

                              there is a reason for the UPLOAD_ERR_OK constant.

                              Comment

                              Working...