0

FORM HTML :

<form name="upload" method="post" action="send.php" enctype="multipart/form-data">

File #1 : <input type="file" name="one" />
File #2 : <input type="file" name="two" />
File #3 : <input type="file" name="three" />
<input type="submit" value="send">

PHP CODE :

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', __DIR__);  
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
 $app = JFactory::getApplication('site');

$input = JFactory::getApplication()->input;
$file1 = $input->files->get('one');
$file2 = $input->files->get('two');
$file3 = $input->files->get('three'); 


$filename1 = JFile::makeSafe($file1['name']); 
$filename2 = JFile::makeSafe($file2['name']);
$filename3 = JFile::makeSafe($file3['name']);

$src = $file['tmp_name'];
$dest1 = JPATH_BASE . "/screens/" . $filename1;
$dest2 = JPATH_BASE . "/screens/" . $filename2;
$dest3 = JPATH_BASE . "/screens/" . $filename3;

if (JFile::upload($src, $dest1, $dest2, $dest3)) {
echo "upload successful";
}
else
{
echo "something went wrong";
}
?>

Getting a blank page? Any suggestions?

0

2 Answers 2

4

One error is $src = $file['tmp_name'];. $file isn't defined. You're should use $file1 there. Another thing is that JFile is one of the classes which don't autoload for some reason. So you need to either register it in the autoloader or require_once it.

I would do something similar to what Lodder suggested for the actual uploading part:

JLoader::register('JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php');

$fields = array('one', 'two', 'three');

foreach ($fields as $field)
{
    $file     = $input->files->get($field);
    $src      = $file['tmp_name'];
    $filename = JFile::makeSafe($file['name']);
    $dest     = JPATH_BASE . "/screen/" . $filename;

    if (JFile::upload($src, $dest))
    {
        echo "upload successful";
    }
}

I would also not use a standalone PHP script to do that but use a component or com_ajax for it.

4
  • Syntax error, don't forget the missing semi colon ;)
    – Lodder
    Jun 12, 2014 at 8:57
  • Ouch :) Fixed now.
    – Bakual
    Jun 12, 2014 at 10:13
  • worked like charm..but it echo's "upload successful" thrice? Also, if i upload two files having same name & extention, only 1 file gets uploaded? isn't there any way to auto-rename the file in that case?
    – saibbyweb
    Jun 12, 2014 at 10:26
  • This of course is only the basic process. The messages and fail handling can be improved for sure. You should also make some sanity checks and access checks otherwise someone for sure will upload a PHP file and executes malicious code on your server.
    – Bakual
    Jun 12, 2014 at 12:34
0

You can have each destination as an argument in JFile::upload. It doesn't work like that. You can use a lopp like so:

$app = JFactory::getApplication('site');
$input = $app->input;

$file1 = $input->files->get('one');
$file2 = $input->files->get('two'); 
$file3 = $input->files->get('three');   

$files = array($file1, $file2, $file3);

foreach($files as $file) {
    $filename = JFile::makeSafe($file['name']);
    $src = $file['tmp_name'];
    $dest = JPATH_BASE . "/screen/" . $filename;

    if(JFile::upload($src, $dest)) {
        echo "upload successful";
    }   
}

I've tested this and it works perfectly. It would probably be best you use @Bakual's code as it's cleaner and a little more simple.

7
  • it didn't work @Lodder. still getting a blank page?
    – saibbyweb
    Jun 12, 2014 at 7:31
  • ok let me test when I get into work
    – Lodder
    Jun 12, 2014 at 7:33
  • Probably Joomla doesn't find JFile (it doesn't autoload unfortunately). Also $file['name'][$i] should be $file[$i]['name'] instead.
    – Bakual
    Jun 12, 2014 at 8:52
  • Funny you should say that cause I've just noticed that myself when testing
    – Lodder
    Jun 12, 2014 at 8:53
  • @Saibbyweb - See my updated code. tested an all working.
    – Lodder
    Jun 12, 2014 at 9:10

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.