Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need help with wp_handle_upload WordPress

  • SOLVED

I'm giving my theme options page a bit of a facelift and replacing my text input fields for links to images with actual image uploaders.

I seem to be having a bit of trouble with wp_handle_upload portion, so I'll outline how I'm doing everything below. For the actual upload file portion I'm using the following:

<input type="file" name="<?php echo 'ifeature['.$value['id'].']'; ?>" id="<?php echo 'if['.$value['id'].']'; ?>" size="40" />

I'm obviously not going to paste my entire theme options code, but the above is placed within a <form method="post" action="options.php" enctype="multipart/form-data"> and the uploader does let me browse and select a file, so that part is working as it should.

Also, without explaining the entire structure of my backend, the variable I am trying to save the file to is if_logo.

Now, here is what I am trying to use to actually upload the file to wp-upload after the user uploads the file and saves the theme options:

$options = get_option('ifeature');
$logo = $options['if_logo'];
$newinput = array();

if ($_FILES[$logo]) {
$overrides = array('test_form' => false);
$file = wp_handle_upload($_FILES[$logo], $overrides);
$newinput['file'] = $file;
}
return $newinput;



However, upon saving I am getting an undefined index error for logo, so the file isn't being saved to if_logo.

This is my first time attempting something like this so I'm sure I'm missing something. I'll be happy to reply with any additional code if it will help answer my question.

Answers (5)

2011-05-14

Christianto answers:

Hi Tyler,
Sorry if I misunderstood your question..
If you want the script run once the file is uploaded. It will return undefined since the image path is not store yet on option table in your WordPress database. Also the global var $_FILES only store value passing by HTTP post request. (cmiiw)

Please try this code,
change 'YOUR_INPUT_TYPE_FILE_NAME' with your input type="file" name attr.

$options = get_option('ifeature');
$newinput = array();

$file_uploaded = $_FILES['YOUR_INPUT_TYPE_FILE_NAME']['tmp_name'];

if ($file_uploaded)) {

$overrides = array('test_form' => false);
$file = wp_handle_upload($file_uploaded, $overrides);

$newinput['file'] = $file;

// save it on option table
$options['if_logo'] = $file['url'];
update_option('ifeature',$options);

}

return $newinput;

If the files is uploaded then the return value of wp_handle_upload (the path of image/url) will be store on your 'ifeature' option.

Tell us the result..
Thanks


Tyler Cunningham comments:

Still getting an undefined index error for if_logo, so it seems that if_logo is not getting used when the file is saved.


Christianto comments:

Tyler,

If you don't mind, can you paste entire options code to pastebin.com so we can check it?
$_FILES are not contain anything, so I think there is no images uploaded..


Tyler Cunningham comments:

Sure thing.

http://pastebin.com/BHLNXjvM

Sorry it's quite long (have quite a few options in this thing). The line's you will need to look at are 173-177, 591-620, and then 964 and on. I know it seems strange putting the function that handles the uploads at the end after the if ( isset( $_REQUEST['updated'] )) but I was having trouble getting other functions to work without doing this, so I just went with what was working.

I'll be happy to answer any other questions you might have. Thank you.


Christianto comments:

Ok, Thanks

First, the var_dump($_FILES) function should be place in the conditional block that run if the form submitted(updated) / or in this case <em>if($_REQUEST['update']){ // function }</em>.
Placing it on switch statement block for displaying HTML form element will return no value since no file is uploaded when WordPress begin to displaying form.

Second, the name of the input type="file" is not "if_logo" but "ifeature[if_logo]", please check output of name attribute in your input tag.

<input type="file" name="<?php echo 'ifeature['.$value['id'].']'; ?>" id="<?php echo 'if['.$value['id'].']'; ?>" size="40" />


Third, the conditional statement of your function that run when the post if updated seems to missing its closing tag so not all script below it will run. For example on line 952 only <em>$options = get_option('ifeature') ;</em> that will be defined when post updated the same thing happen on line 964.

You can try use this code, replacing the code from line 950 to the end, tell us the output on the browser.

if ( isset( $_REQUEST['updated'] )){

$options = get_option('ifeature') ;
$checkimport = $options['if_import_code'];

if ($checkimport != '' ) {

$options = get_option('ifeature') ;
$import = $options['if_import_code'];

$options_array = (unserialize($import));
update_option( 'ifeature', $options_array );
}
}
if ( isset( $_REQUEST['updated'] )){

$options = get_option('ifeature');
$newinput = array();

$file_uploaded = $_FILES['ifeature[if_logo]']['tmp_name'];

if ($file_uploaded) {

$overrides = array('test_form' => false);
$file = wp_handle_upload($file_uploaded, $overrides);

$newinput['file'] = $file;

// save it on option table
$options['if_logo'] = $file['url'];
update_option('ifeature',$options);

}

$var_dump($_FILES);
return $newinput;

}


Tyler Cunningham comments:

Christiano,

I think we're getting on the right track here. The undefined index error is now gone, but the image is not being uploaded to the media library.

How can we check the output of if_logo to see if anything is being saved?


Christianto comments:

I delete $var_dump($_FILES) on above code, its useless since it won't show any output, my fault. Change the code above to..


if ( isset( $_REQUEST['updated'] )){

$options = get_option('ifeature') ;
$checkimport = $options['if_import_code'];

if ($checkimport != '' ) {

$options = get_option('ifeature') ;
$import = $options['if_import_code'];

$options_array = (unserialize($import));
update_option( 'ifeature', $options_array );
}
}
if ( isset( $_REQUEST['updated'] )){

$options = get_option('ifeature');
$newinput = array();

$file_uploaded = $_FILES['ifeature[if_logo]']['tmp_name'];

$options['test_image'] = $file_uploaded;

if ($file_uploaded) {

$overrides = array('test_form' => false);
$file = wp_handle_upload($file_uploaded, $overrides);

$newinput['file'] = $file;

// save it on option table
$options['if_logo'] = $file['url'];
update_option('ifeature',$options);

}
return $newinput;

}

$checkoptions = get_option('ifeature');
echo 'the value of image uploaded is ', $checkoptions['test_image'],' and image url is ', $checkoptions['if_logo'];

Hope this help..


Tyler Cunningham comments:

Hmm, yeah it doesn't look like the image is getting saved properly, the output was simply: the value of image uploaded is and image url is

Something else must be missing here, I'm really not sure what though your code looks pretty solid.


Christianto comments:

Change input type submit with code below on line 470, 603 and 806

// line 470 and 603
<p class="submit" style="clear:left;">
<input type="submit" class="button-primary" value="Save Settings" />
<input type="hidden" name="action" value="save" />
</p>

// line 806
<p class="submit">
<input type="submit" class="button-primary" value="Save Settings" />
<input type="hidden" name="action" value="save" />
</p>

Notice I add another hidden input with 'action' as name and 'save' as value.

and change my code before to

if ( $_POST['action'] == 'save'){

$options = get_option('ifeature') ;
$checkimport = $options['if_import_code'];

if ($checkimport != '' ) {

$options = get_option('ifeature') ;
$import = $options['if_import_code'];

$options_array = (unserialize($import));
update_option( 'ifeature', $options_array );
}
}
if ( $_POST['action'] == 'save'){

$options = get_option('ifeature');
$newinput = array();

$file_uploaded = $_FILES['ifeature[if_logo]']['tmp_name'];

$options['test_image'] = 'test';

if ($file_uploaded) {

$overrides = array('test_form' => false);
$file = wp_handle_upload($file_uploaded, $overrides);

$newinput['file'] = $file;

// save it on option table
$options['if_logo'] = $file['url'];
update_option('ifeature',$options);

}
return $newinput;

}

$checkoptions = get_option('ifeature');
echo 'the value of image uploaded is ', $checkoptions['test_image'],' and image url is ', $checkoptions['if_logo'];

Let see if its work..


Tyler Cunningham comments:

That didn't work, and even if it had I couldn't have used it because of the undefined index that resulted from it, WordPress wouldn't have approved it because of that (as stupid as that sounds, they're that picky).


Christianto comments:

What is the message error output?


Tyler Cunningham comments:

Notice: Undefined index: action in /Users/Dillinger/Sites/wordpress/wp-content/themes/ifeaturepro/library/options/options.php on line 976

Obviously this error will only show with debugging on, but that's how I have to be developing. In any case the code still isn't working for some reason.


Christianto comments:

Change

$file_uploaded = $_FILES['ifeature[if_logo]']['tmp_name'];

To

$file_uploaded = $_FILES['ifeature[if_logo]'];


Tyler Cunningham comments:

Also no luck.


Christianto comments:

Hi Tyler,

I've send you your options.php, There are WP files that should be included on the function you can see it at the end of the options.php. This contain wp_handle_upload function so it won't turn undefined function error.

I change <em>if ( isset( $_REQUEST['updated'] ))</em> to <em>if ( $_POST['action'] == 'save')</em>.

And for the input type="file" I change the name of the input from <em>'ifeature['.$value['id'].']'</em> to only <em>$value['id']</em>, $_FILES seems not treat ifeature[if_logo] correctly and result in empty value.

Please delete code at the end of options.php if the image uploaded correctly, it just for test purpose.

$checkoptions = get_option('ifeature');
echo 'image url is ', $checkoptions['if_logo'];


Tell me the result in your site.
Thanks

Christianto


Tyler Cunningham comments:

Christiano,

Thank you for helping me deal with this via email, we now have it working. I only made two small changes to your code.

I changed if ( $_POST['action'] == 'save'). to if (isset( $_POST['action']) == 'save' to eliminate the unidentified index error which was still present, and added wp_redirect( 'themes.php?page=theme_options' ); right after to bring the user back to the theme options page as it was leaving me on options.php.

Thank you all for your help, and most especially to Christiano.

Cheers.

2011-05-14

Peter Michael answers:

What is the output of var_dump($_FILES) ?


Tyler Cunningham comments:

array(0) { }

2011-05-14

Denzel Chia answers:

Hi,

I am sure that your name attribute in the following file upload input code is wrong.


<input type="file" name="<?php echo 'ifeature['.$value['id'].']'; ?>" id="<?php echo 'if['.$value['id'].']'; ?>" size="40" />


Not sure what echo 'ifeature['.$value['id'].']'; outputs, but it should be the same as that of your wp_handle_upload $_FILES[$logo] attribute.

Assuming that you are using settings api, since you are posting to options.php.
and assuming that your registered setting is ifeature
Try using the following code.


<input type="file" name="ifeature[if_logo]" id="ifeature[if_logo]" size="40" />


Thanks.
Denzel


Tyler Cunningham comments:

Gave me the same error as I was having before.

2011-05-14

AdamGold answers:

Assuming you want it to be dynamic, please add the following code before your file input, so we can see what the file input name is. You will see some text before the input field, please post it here.
var_dump($value['id']);


Tyler Cunningham comments:

string(7) "if_logo" is the output.

2011-05-14

bhuthecoder answers:

// Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
$logo='ifeature['.$value['id'].']';
$uploaded_file = wp_handle_upload($_FILES[$logo], $upload_overrides);

// If the wp_handle_upload call returned a local path for the image
if(isset($uploaded_file['file'])) {

// The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
$file_name_and_location = $uploaded_file['file'];


try this


Tyler Cunningham comments:

That shot back the following errors:

Notice: Undefined variable: value in /Users/Dillinger/Sites/wordpress/wp-content/themes/ifeaturepro/library/options/options.php on line 970

Fatal error: Call to undefined function wp_handle_upload() in /Users/Dillinger/Sites/wordpress/wp-content/themes/ifeaturepro/library/options/options.php on line 972