www.webdeveloper.com
Results 1 to 4 of 4

Thread: $_FILES array only has three elements in it

  1. #1
    Join Date
    Feb 2013
    Posts
    4

    $_FILES array only has three elements in it

    Hi there.

    I want to know what file type I'm uploading. In fact, I know what it is, it's an image/jpeg, but for some reason it's not coming up in my $_FILES array.

    This is the output when I print_r(FILES)
    Array ( [image] => Array ( [name] => Az 036_small.jpg [type] => [tmp_name] => [error] => 2 [size] => 0 ) )
    As you can see, plainly from the name of the file, it's an image/jpeg, but can anyone tell me why the [type] element is empty?

    Here is the code for my upload class if it helps:
    Code:
    <?php
    class sw_Upload {
    	
      	protected $_uploaded = array();
      	protected $_destination;
      	protected $_max = 51200;
      	protected $_messages = array();
      	protected $_permitted = array('image/gif',
    								'image/jpeg',
    								'image/pjpeg',
    								'image/png');
      	protected $_renamed = false;
    
    	public function __construct($path) {
    		if (!is_dir($path) || !is_writable($path)) {
    	  		throw new Exception("$path must be a valid, writable directory.");
    		}
    		$this->_destination = $path;
    		$this->_uploaded = $_FILES;
    		print_r($_FILES);
    	}
    
    	public function move() {
    		$field = current($this->_uploaded);
    		$OK = $this->checkError($field['name'], $field['error']);
    		if ($OK) {
    	  	echo '<br />'.$_FILES['image']['size'];
    	  	$sizeOK = $this->checkSize($field['name'], $field['size']);
    	  	$typeOK = $this->checkType($field['name'], $field['type']);
    	  	if ($sizeOK && $typeOK) {
    			$success = move_uploaded_file($field['tmp_name'], $this->_destination . $field['name']);
    			if ($success) {
    		  		$this->_messages[] = $field['name'] . ' uploaded successfully';
    				} else {
    		  		$this->_messages[] = 'Could not upload ' . $field['name'];
    				}
    	  		}
    		}
    	}
    
      	public function getMessages() {
    		return $this->_messages;
    	}
      
      	protected function checkError($filename, $error) {
    		switch ($error) {
    	  	case 0:
    			return true;
    	  	case 1:
    	  	case 2:
    	    	$this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize();
    			return true;
    	  	case 3:
    			$this->_messages[] = "Error uploading $filename. Please try again.";
    			return false;
    	  	case 4:
    			$this->_messages[] = 'No file selected.';
    			return false;
    	  	default:
    			$this->_messages[] = "System error uploading $filename. Contact webmaster.";
    			return false;
    		}
    	}
    
    	public function getMaxSize() {
    		return number_format($this->_max/1024, 1) . 'kB';
    	}
    
      	protected function checkSize($filename, $size) {
    		if ($size == 0) {
    	  	return false;
    		} elseif ($size > $this->_max) {
    	  	$this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize();
    	  	return false;
    		} else {
    	  	return true;
    		}
    	}
      
      	protected function checkType($filename, $type) {
    		if (!in_array($type, $this->_permitted)) {
    	  	$this->_messages[] = "$filename is not a permitted type of file.";
    	  	return false;
    		} 	else {
    	  	return true;
    		}
    	}

  2. #2
    Join Date
    Feb 2013
    Posts
    4
    Actually, this is better....

    PHP Code:

        <?php
    class sw_Upload {
        
          protected 
    $_uploaded = array();
          protected 
    $_destination;
          protected 
    $_max 51200;
          protected 
    $_messages = array();
          protected 
    $_permitted = array('image/gif',
                                    
    'image/jpeg',
                                    
    'image/pjpeg',
                                    
    'image/png');
          protected 
    $_renamed false;

        public function 
    __construct($path) {
            if (!
    is_dir($path) || !is_writable($path)) {
                  throw new 
    Exception("$path must be a valid, writable directory.");
            }
            
    $this->_destination $path;
            
    $this->_uploaded $_FILES;
            
    print_r($_FILES);
        }

        public function 
    move() {
            
    $field current($this->_uploaded);
            
    $OK $this->checkError($field['name'], $field['error']);
            if (
    $OK) {
              echo 
    '<br />'.$_FILES['image']['size'];
              
    $sizeOK $this->checkSize($field['name'], $field['size']);
              
    $typeOK $this->checkType($field['name'], $field['type']);
              if (
    $sizeOK && $typeOK) {
                
    $success move_uploaded_file($field['tmp_name'], $this->_destination $field['name']);
                if (
    $success) {
                      
    $this->_messages[] = $field['name'] . ' uploaded successfully';
                    } else {
                      
    $this->_messages[] = 'Could not upload ' $field['name'];
                    }
                  }
            }
        }

          public function 
    getMessages() {
            return 
    $this->_messages;
        }
      
          protected function 
    checkError($filename$error) {
            switch (
    $error) {
              case 
    0:
                return 
    true;
              case 
    1:
              case 
    2:
                
    $this->_messages[] = "$filename exceeds maximum size: " $this->getMaxSize();
                return 
    true;
              case 
    3:
                
    $this->_messages[] = "Error uploading $filename. Please try again.";
                return 
    false;
              case 
    4:
                
    $this->_messages[] = 'No file selected.';
                return 
    false;
              default:
                
    $this->_messages[] = "System error uploading $filename. Contact webmaster.";
                return 
    false;
            }
        }

        public function 
    getMaxSize() {
            return 
    number_format($this->_max/10241) . 'kB';
        }

          protected function 
    checkSize($filename$size) {
            if (
    $size == 0) {
              return 
    false;
            } elseif (
    $size $this->_max) {
              
    $this->_messages[] = "$filename exceeds maximum size: " $this->getMaxSize();
              return 
    false;
            } else {
              return 
    true;
            }
        }
      
          protected function 
    checkType($filename$type) {
            if (!
    in_array($type$this->_permitted)) {
              
    $this->_messages[] = "$filename is not a permitted type of file.";
              return 
    false;
            }     else {
              return 
    true;
            }
        }

  3. #3
    Join Date
    Feb 2013
    Posts
    4
    My issue is, the line of code in this function below that starts with $typeOK is returning false from the checkType function.
    PHP Code:
    public function move() {
            
    $field current($this->_uploaded);
            
    $OK $this->checkError($field['name'], $field['error']);
            if (
    $OK) {
              echo 
    '<br />'.$_FILES['image']['size'];
              
    $sizeOK $this->checkSize($field['name'], $field['size']);
              
    $typeOK $this->checkType($field['name'], $field['type']);
              if (
    $sizeOK && $typeOK) {
                
    $success move_uploaded_file($field['tmp_name'], $this->_destination $field['name']);
                if (
    $success) {
                      
    $this->_messages[] = $field['name'] . ' uploaded successfully';
                    } else {
                      
    $this->_messages[] = 'Could not upload ' $field['name'];
                    }
                  }
            }
        } 
    Here is the checkType function.
    PHP Code:
        protected function checkType($filename$type) {
            if (!
    in_array($type$this->_permitted)) {
              
    $this->_messages[] = "$filename is not a permitted type of file.";
              return 
    false;
            }     else {
              return 
    true;
            }
        } 
    but as you can see from the _permitted array up the top of my class definition in my previous post, 'image/jpeg' should be allowed. This function is looking at the [type] field in the FILES array, and because it's empty, it's returning false, where I'm thinking that the [types] field in the FILES array should have something like 'image/jpeg' in it so the checkTypes function can make a comparison.

    David.

  4. #4
    Join Date
    Feb 2013
    Posts
    4
    Don't worry about this post. I have just learned that this is not the most effective way to do an upload class.
    cheers.
    David.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles