Fixing "Undefined offset" when working with upload forms

I'm working on building a file uploader, my first, and am getting an error that (no matter what I do) I can't fix! It is returning an undefined offset telling me that there is nothing in the $_FILES array ... but I selected photos (it's just not retrieving the data)! Below you will find all the code that appears to relate to this issue. I have tried to debug this on my own but with absolutely no success.

Update

After implementing @Dave's suggestions there were no more offset errors ... however, the only thing getting sent to the $_POST is the max_file_size and nothing is getting sent to the $_FILES arrays. I have included the return as well as the updated code below.

Here's the return I'm dealing with:

// From print_r( $_POST );
Array ( [MAX_FILE_SIZE] => 33554432 [upload] => )    

// From print_r( $_FILES );
array(1) { ["photos"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } 
["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) 
{ [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> 
array(1) { [0]=> int(0) } } }

// From var_dump( $_FILES ); just to be sure my bases are covered
array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) 
{ [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } 
["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } }

// Return from my else showing that $_FILES['photos']['name'] is empty
Nope this doesn't work either!

Here's my code

HTML form:

<form id="photo-upload" method="POST" action="<?php echo plugins_url( 'photo-uploadr/templates/upload.php' ); ?>" enctype="multipart/form-data">                        

    <input type="hidden" name="MAX_FILE_SIZE" value="33554432" />               
    <input type="file" id="file-upload" name="photos[]" multiple />
    <button type="button" class="btn" id="pick-photos"><?php _e( "Select Photos to Upload", "shorti" ); ?></button>

    <ul id="file-uploads">
        <!-- The file uploads will be shown here -->
    </ul>

    <button name="upload" id="upload" class="btn upload"><?php _e("Upload", "shorti"); ?></button>

</form>

PHP file handler (upload.php):

<?php 

require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php" );
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php" );
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php" );

var_dump( $_FILES );

if ( !$_FILES ) exit;

// Required for the wp_handle_upload() to upload the files
$upload_overrides = array( 'test_form' => false );

global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;

// load up a variable with the upload directory
$uploads = wp_upload_dir();

$uploaded = count( $_FILES['photos']['name'] );
for ( $i=0; $i<$uploaded; $i++ ) {
$file_array = array(
    'name'      => $_FILES['photos']['name'][$i],
    'type'      => $_FILES['photos']['type'][$i],
    'tmp_name'  => $_FILES['photos']['tmp_name'][$i],
    'error'     => $_FILES['photos']['error'][$i],
    'size'      => $_FILES['photos']['size'][$i]
);
// this file continues for another 30 lines ... 
// but this is where the error is occuring so why add more?

end Update

Like I said I am new to uploading files from a form ... I am using this code to try and understand uploading ... I'm sure it will be something really simple that I just don't see.

I really need to have this fixed today if at all possible ... any help, however, is appreciated! Thanks

Answers


give this a try see if this works its only really a variation but I've had problems with foreach and range previously

$uploaded = count($_FILES['photos']['name']);
for ($i=0;$i<$uploaded;$i++) {
    $file_array = array(
    'name'      => $_FILES['photos']['name'][$i],
    'type'      => $_FILES['photos']['type'][$i],
    'tmp_name'  => $_FILES['photos']['tmp_name'][$i],
    'error'     => $_FILES['photos']['error'][$i],
    'size'      => $_FILES['photos']['size'][$i]
    );
    //rest of your logic code here
}

Because there is 1 file, the following is producing Array ( [0] => 0 [1] => 1 ):

range( 0, $count_files ) // range( 0, 1 )

This is incorrect; the script is trying to find 2 files. You need to change it to:

range( 0, $count_files - 1 )

I think you must iterate till $count_files - 1, because if you upload 1 file your foreach loop will currently execute twice.


Need Your Help

password request has expired in magento ver 1.9.0.1

magento passwords request expired-sessions

i have created a whole website on my test server then moved it to client server. After clicking the "Forgot password" option, i got link on my email . When i click on that link it redirects me to r...