1

I'm developing a Joomla component, which should provide the functionality to upload files. I followed the guidelines from docs.joomla.org, and derived the following function:

public function fileupload()
{

    $jinput = JFactory::getApplication()->input;
    $files = $jinput->files->get('jform','array',null);

    $fileError=$files['image']['error'];

    If ($fileError>0){

        switch (true){  

            case $fileError==1:
            echo JText::_( 'FILE TO LARGE THAN PHP INI ALLOWS' );
            return;

            case $fileError==2:
            echo JText::_( 'FILE TO LARGE THAN HTML FORM ALLOWS' );
            return;

            case $fileError==3:
            echo JText::_( 'ERROR PARTIAL UPLOAD' );
            return;

            case $fileError==4:
            echo JText::_( 'ERROR NO FILE' );
            return;

        }   
    }

    $filesize=$files['image']['size'];

    If ($filesize>2000000){

    echo JText::_( 'FILE BIGGER THAN 2MB' );    

    }

    $fileTemp=$files['image']['tmp_name'];


    $imageinfo = getimagesize($fileTemp);
    $okMIMETypes = 'image/jpeg,image/jpg,image/pjpeg,image/png,image/x-png,image/gif';
    $validFileTypes = explode(",", $okMIMETypes); 

    if( !is_int($imageinfo[0]) || !is_int($imageinfo[1]) ||  !in_array($imageinfo['mime'], $validFileTypes) )
    {
    echo JText::_( 'INVALID FILETYPE' );
    return;
    }

    $fileName = preg_replace("/[^A-Za-z0-9]/i", "-", $fileName);

    $uploadPath = JPATH_Component.DS.$fileName;

    if(!JFile::upload($fileTemp, $uploadPath)) 
    {
            echo JText::_('ERROR MOVING FILEs'.$fileTemp);
            return;
    }
    else
    {
       // success, exit with code 0 for Mac users, otherwise they receive an IO Error
       exit(0);
    }

    If (isset($files)){
    $name = $files['image']['name'];
    $size = $files['image']['size'];
    }

    $this->setRedirect(JRoute::_('index.php?   
            option=com_name&view=confirmation&filename='.$name.'&filesize='.
            $size.'&tmp='.$fileTemp.'&dir=', false));


        }

The point is, that "JFile::upload($fileTemp, $uploadPath)" does always result in the Error

Can't move File 

The path to the tmp-folder is correct (*/shared-data/webroot/01_Playground/tmp*), in dependence to another post at stackoverflow, I set the file permissions to 644, and the directory permission to 755(I also tried 777), without success so far. $files['image']['tmp_name'] is returning a value, but there is nothing in my tmp-folder. (I tried this code snippet on linux-ubuntu as well as on windows, without success.) Perhaps someone knows a working solution for my Issue?

Update: After activating error-reporting and using var_dump, I figured out the following: It has something to do with exit(0); I removed the line containing exit(0) and it works as it should be.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • have you tried using `var_dump();` with some of the variable to ensure they are functional? – Lodder Aug 28 '13 at 20:08
  • 2 Questions: What does your form look like? And where is the `fileupload()` method? – Craig Aug 29 '13 at 03:45
  • I don't know if you followed the recent security release, but you need to do more extensive checking on the file. Is there a good reason not to just use JFormFiledMedia or use JMediaHelper? – Elin Aug 29 '13 at 10:47
  • @ Elin: looks like a useful hint. Currently I'm tailoring the com_media component to fit my requirements. – user2665206 Aug 31 '13 at 10:22

1 Answers1

0

Your file is not uploaded to the Joomla tmp folder but in the PHP tmp folder.

Thus your code should look like:

$fileTemp=ini_get('upload_tmp_dir') . $files['image']['tmp_name'];
Riccardo Zorn
  • 5,590
  • 1
  • 20
  • 36