MyBB Community Forums

Full Version: Can't access $_FILES
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Does anyone have any ideas/suggestions as to why I can't access the $_FILES array in my plugin?

I'm creating a plugin that allows certain mods to fill out a form, which includes uploading a file. All parts of the form sequence work except the upload; I get upload['error']=1.

Here's the relevant code. Anyone see anything?

Thanks.

				if ($mybb->request_method == 'post') {			
					// if uploading
					if ($mybb->input['upload']) {	
						// move file/save to db
						require_once(MYBB_ROOT.'inc/functions_upload.php');
						$upload = upload_file($_FILES['plan_docs'], MYBB_ROOT.'uploads/plandocs');
						
						// check for empty file or error
						if ($upload['error']) {
							if ($upload['error'] == 1) {
								$message = "You must select a file to upload.";	
								$title = "";
							} else {
								$message = "Could not move file. Please try again or inform site administrator.";
								$title = "System Error";
							}
							$data .= inline_error($message, $title);
						} else {
							// update db
							$doc = array('filename' => $upload['filename'], 'cid' => $mybb->input['cid']);
							$result = $db->insert_query('plan_docs', $doc);
							if ($result === false) {
								$data .= inline_error('Could not log filename. Please try again or inform site administrator.', 'System Error');
							} else {
								// show message
								$data .= '<div class="success"><p class="success_message"><span class="text">Document has been successfully uploaded.</span></p></div>';
							}
						}
					} else {
					// if finished
						// redirect
						redirect('modcp.php', 'This completes the process. Please alert site administrator.');
						break;
					}
				}
Could you post the source code of the form? I'm guessing you've forgotten enctype="multipart/form-data".
(2012-08-27, 04:08 PM)euantor Wrote: [ -> ]I'm guessing you've forgotten enctype="multipart/form-data".

Nope, it's there. I checked that.
Damn. Have you tried lobbing a var_dump($_FILES) in there to see if there's anything in the array at all?

EDIT: the issue may be because you've basically got a form within a form. Try add this:

<input type="hidden" name="action" value="implementation" />
Empty

var_dump gets: array(0) { }

print_r gets: Array()
See my quick edit just above Smile
(2012-08-27, 04:16 PM)euantor Wrote: [ -> ]EDIT: the issue may be because you've basically got a form within a form. Try add this:

<input type="hidden" name="action" value="implementation" />

Got that in there already.

I also have several other hidden fields in there, ie. 'page' variable and 'cid' (both of which are required to even show the form).

Here's how I make the form..
I use my own form creator class, set up arrays of inputs and call the form creator.

try {
					$inputs = array(
						0 => array(
							'description' => '<tr><td class="trow1" width="25%"><strong>Upload plan documents:</strong></td><td class="trow1" width="75%">',
							'type' => 'file',
							'name' => 'plan_docs',
							'break_after' => false,
						),
						1 => array(
							'description' => '',
							'type' => 'hidden',
							'name' => 'action',
							'value' => 'implementation',
							'break_after' => false,
						),
						2 => array(
							'description' => '',
							'type' => 'hidden',
							'name' => 'cid',
							'value' => $mybb->input['cid'],
							'break_after' => false,
						),
						3 => array(
							'description' => '',
							'type' => 'hidden',
							'name' => 'page',
							'value' => 'docs',
							'break_after' => false,
						),
						4 => array(
							'description' => '',
							'type' => 'submit',
							'name' => 'upload',
							'value' => 'Upload',
						),
						5 => array(
							'description' => '</td></tr></tr></table><div align="center"><br />',
							'type' => 'submit',
							'name' => 'submit',
							'value' => 'Finish',
						),
					);
					
					// add form data
					$form_data = array(
						'method' => 'POST',
						'action' => 'modcp.php?action=implementation',
					);
					
					// add the form
					$creator = new FormCreator;		
					if (isset($_POST)) {
						$form = $creator->create($inputs, $form_data, $_POST);
					} else {
						$form = $creator->create($inputs, $form_data);		
					}
					// add the form
					$data .= '<table border="0" cellspacing="1" cellpadding="4" class="tborder"><tr><td class="thead" colspan="2"><strong>Plan Documents</strong></td>';
					$data .= $form->build();
					$data .= '</div>';
				} catch (Exception $e) {
					$data .= inline_error('Could not build the form. Please try again or inform tech support.', 'System Error');
				}
				break;


Also (if this makes any difference), I previously wrote it as just an additional file that goes in the root and runs as myboard.com/filename. When I run this file as just an additional file everything works. When I try to make it as a plugin, I lose the $_FILES.


Thanks for the help btw..
Weird indeed. I've never worked with file uploads within a plugin but you should definitely be able to access the $_FILES variable just fine seeing as it's a super global unless MyBB does something weird with it within the plugins class. I'll have to look into that.

EDIT: Actually, just thinking out loud here but is the $_FILES array possibly accessible via $mybb->input? I'm not entirely sure my self as I've never tried working with files in a plugin as I said.
(2012-08-27, 04:33 PM)euantor Wrote: [ -> ]Weird indeed.
Agreed

(2012-08-27, 04:33 PM)euantor Wrote: [ -> ]I've never worked with file uploads within a plugin but you should definitely be able to access the $_FILES variable just fine seeing as it's a super global
I thought so too - even tried adding $_FILES to the global definition in my function - no change.

(2012-08-27, 04:33 PM)euantor Wrote: [ -> ]unless MyBB does something weird with it within the plugins class. I'll have to look into that.
That's a good idea, I'm going to look in that class.

Thanks for helping so far, I appreciate it.
Check my edit again ;P You just beat me to replying.
Pages: 1 2