Joomla JFile::upload Issue

2.1k views Asked by At

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.

1

There are 1 answers

0
Riccardo Zorn On

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'];