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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.18427 seconds
  • Memory Usage 2,917KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates