1
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 ( strtolower(JFile::getExt($filename) ) == 'jpg') {
 if (JFile::upload($src, $dest))
{
    echo "upload successful";
}
}
else {
header("Location: /error.php"); 
}

The problem i am facing is that when i use the above code :

1) When i try to upload any non-jpg file, ELSE Statement is executed :)

else {
header("Location: /error.php"); 
}

but when i use this IF-STATEMENT instead of the above mentioned one. :

if (JFile::upload($src, $dest))
{
    header("Location: /done.php");
}

and then when i try to upload any non-jpg file, it jumps to IF-STATEMENT :

if (JFile::upload($src, $dest))
{
    header("Location: /done.php");
}

Why is this happening?

Just when i change echo "upload successful"; to header("Location: /done.php"); Why does the event change?

1

Try this:

$app = JFactory::getApplication();

if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
    if (JFile::upload($src, $dest))
    {
        $app->setHeader('Location', JUri::root() . 'modules/mod_mymodule/done.php');
        // Success
    }
    else 
    {
        $app->setHeader('Location', JUri::root() . 'modules/mod_mymodule/error.php'); 
        // File could not be uploaded
    }   
}
else 
{
    $app->setHeader('Location', JUri::root() . 'modules/mod_mymodule/error.php'); 
    // Incorrect file type. Please select a jpg
}
  • it gives a blank page! but just when i changed $app->setHeader('Location', JUri::root() . 'modules/mod_mymodule/done.php'); to echo "something"; it echo'ed something even when i uploaded a .PNG FILE (but the .png was not uploaded!) – saibbyweb Jun 19 '14 at 10:34
  • 1
    You keep getting blank pages when things don't work. Please enable error report and set it to maximum or development. As for the redirect code I provided, did you change the path according to where those 2 files are located? JUri::root() defines the root of your Joomla site to change the path after that relative to where ever your files are – Lodder Jun 19 '14 at 10:42
  • Try this instead and see if it works: $app->redirect(JUri::root() . 'modules/mod_mymodule/done.php', 'Success'); – Lodder Jun 19 '14 at 10:54

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.