CakePHP:调用成员函数

时间:2013-07-03 14:06:08

标签: php cakephp

我在CakePHP中开发了一个帮助程序来上传文件,但是当我在操作中使用它时,它会返回错误。 我知道这个错误意味着什么,我试着在控制器中用public $helpers = array('Session', 'File', 'Html');调用帮助器,但是发生了同样的事情。我不知道该怎么办。 错误:

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";
    }
}
}
?>

错误行:

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

1 个答案:

答案 0 :(得分:1)

重要的一点是,您希望将帮助用作组件。 Helper仅用于您可以浏览的视图并强制使用它,但是,您必须将上传逻辑移动到该行为。处理上传不是视图层责任。

http://book.cakephp.org/2.0/en/controllers/components.html可从控制器访问。

http://book.cakephp.org/2.0/en/models/behaviors.html,例如文件上传: https://github.com/webtechnick/CakePHP-FileUpload-Plugin/blob/master/models/behaviors/file_upload.php

帮助控制器的视图组件。