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.

3
  • 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
    Commented Jun 10, 2014 at 14:30
  • i tried using the global PHP $_FILES array method, but it doesn't work while using JInput!
    – saibbyweb
    Commented Jun 10, 2014 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
    Commented Jun 10, 2014 at 15:11

2 Answers 2

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

5
  • is this the path where the file will be uploaded "/modules/mod_mymodule/" ?
    – saibbyweb
    Commented Jun 11, 2014 at 17:43
  • @Saibbyweb - yes
    – Lodder
    Commented Jun 11, 2014 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
    Commented Jun 11, 2014 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
    Commented Jun 11, 2014 at 18:00
  • Maybe you could add code on how to get error message if the upload failed? Commented Apr 27, 2018 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');
2

Your Answer

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

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