4

I have an input form in the admin view:

<form name="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file_upload" />
    <input type="submit" value="Upload Image" />
    <input type="hidden" name="option" value="com_newsshowcase" />
    <input type="hidden" name="task" value="upload" />
    <?php echo JHtml::_( 'form.token' ); ?>
</form>

This form calls the function upload() in the controller:

public function upload()
{
    // Check for request forgeries
    JSession::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));
    $app = JFactory::getApplication()->input;
    $file = $app->files->get('file_upload');
    $filename = JFile::makeSafe($file['name']);
}

I've been echoing(debugging) the $filename and the $file variables, but no value is being echoed. When I echo something like:

echo '<pre>',print_r($file,1),'</pre>';

nothing is echoed. I assume that this means that the file to-be uploaded by this function is not being passed from the view to the controller?

Update: I also imported JInput explicitly (I saw mixed things on this in my searches saying that it isn't necessary, but wouldn't hurt). My original controller extended JController, I tried changing this to JControllerForm and changed the import dependency accordingly and still nothing.

Update #2: When I do a var_dump on $_POST I can see the form data, but when I do a var_dump on $_FILES it shows an empty array.

Update #3: The size of the image I am trying to upload, as a test, is 345 bytes(super small).

Update #4: I duplicated the form (removed the Joomla specific fields) in a separate stand-alone PHP file that linked to a PHP endpoint that did a var_dump($_FILES) and it displayed the upload data.

Update #5: Not sure if it matters, but the HTML form resides in the "default_body" section of the admin page.

11
  • What is the file path and classname of the controller? Sep 23, 2014 at 2:11
  • Class name is: class NewsShowcaseController extends JController, "News Showcase" is the name of the custom component I'm building, path is: administrator/components/com_newsshowcase/controller.php
    – Eric
    Sep 23, 2014 at 2:18
  • If you put a statement like echo 'Anything'; in that upload function (toward the top), that is showing up, right? Sep 23, 2014 at 3:21
  • Also just double checked all the code, and everything works for me. I'd check the file size of the file that you are uploading. It may exceed your php.ini limits. Sep 23, 2014 at 3:26
  • When I put a 'echo "test";' after the $filename line it will show the word "test" printed out. I tried a smaller image and still no luck. I tried a couple of things that I added to the original post that, unfortunately, still didn't help.
    – Eric
    Sep 23, 2014 at 11:14

1 Answer 1

2

A couple of things. First, verify this is a bug by using the deprecated but still working JRequest to get the file data. I've had to resort to using this method myself recently for a file upload need.

$file = JRequest::getVar('file_upload', null, 'files', 'array'); 

If that doesn't work still, simply try dumping the $_FILES['file_upload'] request variable to confirm the file is being sent. My thought is using JRequest will work.

Secondly, you should attach the upload method to the specific controller for the view. Although you should and can reach a public method in your primary entry point, you should create and attach to a controller which will utilize the model/view.

In addition, once the task is migrated to the proper controller, no need to retrieve a JInput object as its already part of the parent class.

For arguments sake, lets say the view is Files, the controller look like so:

NewsShowcaseControllerFiles extends JControllerLegacy {

    public function upload() {
        jimport('joomla.filesystem.file');
        jimport('joomla.filesystem.folder');
        // Check for request forgeries
        JSession::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));

        // Notice in the controllers context, no need to instansiate Application Object
        $file = $this->input->files->get('file_upload');
        $filename = JFile::makeSafe($file['name']);

        // logic to save file or dump data to verify success
    }

}

In the primary controller entry point located in the components site/admin root add the following line to your display method to register your task:

$this->registerTask('files.upload', 'upload');

Last, you must alter the endpoint URL for the form or AJAX request handling the form submission:

JRoute::_('index.php?option=com_newsshowcase&task=files.upload');
16
  • While this is probably a better coding practice for Joomla, I'm not sure it helps at all with why he can't get the file information. Sep 23, 2014 at 19:55
  • 1
    Excellent observation, edited answer to start with something specific for OP's issue. Sep 23, 2014 at 20:27
  • Thank you for your response, but I am getting a NULL when I do a var_dump on the JRequest $file you suggested at the top of your answer
    – Eric
    Sep 23, 2014 at 23:33
  • I'm also getting a NULL when I do a $_FILES['file_upload'] var_dump unfortunately
    – Eric
    Sep 23, 2014 at 23:34
  • What about if you var_dump the $_FILES array? Sep 23, 2014 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.