Notice: Undefined index Error. Help!!!

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

    #16
    Oh. Thanks Dormilich. Sorry for the late reply. I have not been feeling too fine. This upload page was just a test, on how i could handle my uploads. When i was sure i could upload successfully, i wrote 2 public methods SAVE and ATTACH_FILE, that perfectly handles from file upload, to tmp_directory, to permanent directory, considering every exception i could think of. I flexed the muscles of the conditional statements on this one. Here are they. Anything i should add??


    Code:
    public function attach_file($file) {
    		// Perform error checking on the form parameters
    		if(!$file || empty($file) || !is_array($file)) {
    		  // error: nothing uploaded or wrong argument usage
    		  $this->errors[] = "No file was uploaded.";
    		  return false;
    		} elseif($file['error'] != 0) {
    		  // error: report what PHP says went wrong
    		  $this->errors[] = $this->upload_errors[$file['error']];
    		  return false;
    		} else {
    			// Set object attributes to the form parameters.
    		  $this->temp_path  = $file['tmp_name'];
    		  $this->filename   = basename($file['name']);
    		  $this->type       = $file['type'];
    		  $this->size       = $file['size'];
    			// Don't worry about saving anything to the database yet.
    			return true;
    
    		}
    	}
      
    	public function save() {
    		// A new record won't have an id yet.
    		if(isset($this->id)) {
    			// Really just to update the caption
    			$this->update();
    		} else {
    			// Make sure there are no errors
    			
    			// Can't save if there are pre-existing errors
    		  if(!empty($this->errors)) { return false; }
    		  
    			// Make sure the caption is not too long for the DB
    		  if(strlen($this->caption) <= 255) {
    				$this->errors[] = "The caption can only be 255 characters long.";
    				return false;
    			}
    		
    		  // Can't save without filename and temp location
    		  if(empty($this->filename) || empty($this->temp_path)) {
    		    $this->errors[] = "The file location was not available.";
    		    return false;
    		  }
    			
    			// Determine the target_path
    		  $target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;
    		  
    		  // Make sure a file doesn't already exist in the target location
    		  if(file_exists($target_path)) {
    		    $this->errors[] = "The file {$this->filename} already exists.";
    		    return false;
    		  }
    		
    			// Attempt to move the file 
    			if(move_uploaded_file($this->temp_path, $target_path)) {
    		  	// Success
    				// Save a corresponding entry to the database
    				if($this->create()) {
    					// We are done with temp_path, the file isn't there anymore
    					unset($this->temp_path);
    					return true;
    				}
    			} else {
    				// File was not moved.
    		    $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
    		    return false;
    			}
    		}
    	}

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #17
      on line #8 I’d use elseif($file['error'] !== \UPLOAD_ERR_OK), that way it becomes immediately clear what the test is for.

      and on line #2 I’d use type hinting: public function attach_file(arr ay $file) {

      Comment

      • talk2tim
        New Member
        • Feb 2015
        • 19

        #18
        Thanks Dormilich, on Line #8 i kinda prefer the numeric error codes. Thats how they are understandable to me.

        Line #2 is also a good idea.

        Thanks Dormilich, you have really been helpful. :-)

        Comment

        Working...