Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    $_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:

    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;
            }
        }

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    can you post your form please also. Also you do know that using $_FILES type is the worst way to check the type of file, it is entirely unreliable in many cases.

    You might have a look at post 13 on this thread I learned alot from the folks here.

    should i or shouldnt i - upload option

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by durangod View Post
    can you post your form please also. Also you do know that using $_FILES type is the worst way to check the type of file, it is entirely unreliable in many cases.

    You might have a look at post 13 on this thread I learned alot from the folks here.

    should i or shouldnt i - upload option
    No, I didn't know it was a bad way. I'm learning from a book in fact and this code is from it. Actually it's a pretty good book, but I'm open to improving on this since it's not working for me.
    Here is the form:
    Code:
    <?php
    	//set the maximum upload size in bytes
    	$max = 71200;
    	if(isset($_POST['upload'])) {
    		// define the path to the upload folder
    		$destination = 'c:/xampp/upload_test/';
    		// move the file to the upload folder and rename it
    		require_once('../classes/util/Upload.php');
    		try {
    			$upload = new sw_Upload($destination);
    			$upload -> move();
    			$result = $upload -> getMessages();
    		} catch (Exception $e) {
    			echo $e -> getMessage();
    		}
    	}
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Upload File</title>
    </head>
    
    <body>
    	<?php
    		if(isset($result)) {
    			echo '<ul>';
    			foreach ($result as $message) {
    				echo "<li>$message</li>";				
    			}
    			echo '</ul>';
    		}
    	?>
            
    	<form action="" method="post" enctype="multipart/form-data" id="uploadImage">
      		<p>
        		<label for="image">Upload image:</label>
                <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>"> 
                <input type="file" name="image" id="image">
     		</p>
      		<p>
        		<input type="submit" name="upload" id="upload" value="Upload">
      		</p>
    	</form>
    
    </hody>
    </html>
    I will look at that link now.
    Thanks
    PS: the problem is that the checkType function is returning false becauuse it's comparing against the type in the FILES array, and hence it's returning false because it's comparing against an empty element [type].
    David.

  • #4
    New Coder
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whoa, that's a pretty awesome post Durangod.
    Forget this post, and I will see if I can fit your code into the example I'm doing here.
    Thanks a lot.

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Durangod

    I like your code, but there is one thing in there that isn't working for me.
    for fopen(..) you use [tmp_name] which is also another element in my array that is missing.

    So I need to find the location of this uploaded file. Either I'm doing something wrong to get this array filled in, or I have to find a way to find the location of this uploaded file in the tmp directory.

  • #6
    New Coder
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's ok, I have worked out now that if the file is over the file size limit, it doesn't fill in the FILES array.
    So, I can use your code now, I just have to make sure I fail on file size violation first.
    CHeers.
    David.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by Bucephalus View Post
    It's ok, I have worked out now that if the file is over the file size limit, it doesn't fill in the FILES array.
    if you look closely, you see that there is an element named "error" with a value of 2. checking back in the Manual, you find:
    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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