Gabriel Carvalho
Gabriel Carvalho

Reputation: 133

CakePHP: Call to a member function

I was developing a helper in CakePHP to upload files, but when i was using it on the action, it return a error. I know what this error mean, i tried to call the helper with public $helpers = array('Session', 'File', 'Html'); in the controller, but happen the same. I don't know what to do. Error:

Fatal Error

Error: Call to a member function info() on a non-object 
File: /Users/diakonosdigital/Drive Diakonos/Google Drive/eJersusalem/WWW/ejerusalem/branches/app/Controller/DashboardController.php 
Line: 614

FileHelper.php:

        $info['name'] = $File['name'];
        $info['tmp'] = $File['tmp_name'];
        $info['ext']['mime'] = $File['type'];
        $info['ext']['ext'] = $typeq[1];
        $info['size']['b'] = $File['size'];
        $info['size']['kb'] = $File['size'] / 1024;
        $info['size']['mb'] = $info['size']['kb'] / 1024;
        $info['error'] = $File['error'];
        $info['hashed_name'] = $nname;
        if($getHashed):
            return $info['hashed_name'];
        else:
            return $info;
        endif;
    }else{
        return "A file was not specified.";
    }
}

/**
 * Função responsável pelo upload dos arquivos
 * @author Gabriel Cavalho
 * @param $File file required - Deve prover todos os índices que o campo 'file' oferece, tmp_name, name, etc.
 * @param $Options array required - Deve ser um array contendo as opçoes do upload, como o caminho de upload (path), se o nome deve ser 'hashed'
 */ 
public function upload($File, $Options){
    if(isset($File) && isset($Options)){
        if($Options['hash']){
            $File = $this->info($File);
        }
        if(empty($Options['path'])){
            $Options['path'] = WWW_ROOT . 'files' . DS;
        }
        if($Options['hashname']){
            $File['name'] = $File['hashed_name'];
        }
        if($File['size']['mb'] > 5){
            $File['error'] = 5;
        }
        if($File['error'] == 0){
            /**
             * No caso de não haver erros nenhum, essa parte é entrada em ação. 
             */ 
            if(move_uploaded_file($File['tmp'], $Options['path'] . $File['name'])){
                return true;
            }else{
                return "An error has ocurred while uploading file.";
            }
        }else{
            switch ($File['error']) {
                case 1:
                    return "The file size exceeds the size provided by php.ini file";
                    break;
                case 2:
                    return "The file size exceeds the size provided by the form (MAX_FILE_SIZE)";
                    break;
                case 3:
                    return "The file wasn't fully uploaded";
                    break;
                case 4:
                    return "No file was uploaded";
                    break;
                case 5:
                    return "The file wasn't upload successfuly, the file size exceeds 5MB";
                    break;
                case 6:
                    return "Missing tmp folder";
                    break;
                case 7:
                    return "Failed in attempt to write file to disk";
                    break;
                default:
                    return "Failed to upload file";
                    break;
            }
        }
    }else{
        return "Please, provide valid \$File and \$Options";
    }
}
}
?>

The line that the error is:

$this->request->data['MainSlider']['slide'] = $this->File->info($this->data['MainSlider']['slide_file'], true);

Upvotes: 0

Views: 1658

Answers (1)

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

One important thing is that you want to use helper as a component. Helper is meant to use only in views you can hack around and force to use it but, you must move your uploading logic to the behaviour. It's not view layer responsibility to handle uploads.

http://book.cakephp.org/2.0/en/controllers/components.html which is accessible from the controllers.

http://book.cakephp.org/2.0/en/models/behaviors.html, for example file uploading: https://github.com/webtechnick/CakePHP-FileUpload-Plugin/blob/master/models/behaviors/file_upload.php

So helper for view components for controller.

Upvotes: 1

Related Questions