6

i use a html form on one of my articles, i am successful in getting the values from the form using JInput!

$input = JFactory::getApplication()->input;

But now i need to add a file upload input to the form :

<input type="file" name="upload">

How to upload and save files on the server using JInput!?

File upload with restrictions, allowing only specific file extentions.

  • Have you tried any code yet to get the upload working? is File upload with restrictions, allowing only specific file extentions. an error you're getting with your current code? If so, please show you current code – Lodder Jun 10 '14 at 14:30
  • i tried using the global PHP $_FILES array method, but it doesn't work while using JInput! – saibbyweb Jun 10 '14 at 14:58
  • Have a look here. This page uses JRequest so simply replace with your JInput code: docs.joomla.org/How_to_use_the_filesystem_package – Lodder Jun 10 '14 at 15:11
16

In addition to @Bakual's answer, let's assume you a simple form like so:

<form name="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file_upload">
</form>

To upload the file once the submit button has been pressed, you can use this:

$input = JFactory::getApplication()->input;
$file  = $input->files->get('file_upload'); 

// Cleans the name of teh file by removing weird characters
$filename = JFile::makeSafe($file['name']); 

$src  = $file['tmp_name'];
$dest = JPATH_BASE . '/modules/mod_mymodule/' . $filename;

if (JFile::upload($src, $dest)) 
{
      // The file has successfully been uploaded :)
} 
else 
{
      // Oh crap, something happened. Run!
}

You will obviously need to change the path ($dest) to whatever suits your needs.

There is a load of validations you should perform when handling uploads, a lot of which can be found on the link you have been provided with already:

http://docs.joomla.org/How_to_use_the_filesystem_package

  • is this the path where the file will be uploaded "/modules/mod_mymodule/" ? – saibbyweb Jun 11 '14 at 17:43
  • @Saibbyweb - yes – Lodder Jun 11 '14 at 17:47
  • i am using this [$dest = JPATH_BASE . "/screen" . $filename;] and file is getting uploaded in the root directory with a prefix "screen" ? – saibbyweb Jun 11 '14 at 17:58
  • 1
    I believe you should add a forward slash on to the end to it's "/screen/". But yes, if this is the path you're defining then this is where the file will be uploaded – Lodder Jun 11 '14 at 18:00
  • Maybe you could add code on how to get error message if the upload failed? – Olle Härstedt Apr 27 '18 at 11:52
7

There are two tricky things people usually fall over when it comes to file uploads using JInput:

  • Your form tag needs to contain enctype="multipart/form-data"
  • The file array is retrieved with $input->files->get('upload');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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