0

我在 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

文件助手.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);
4

1 回答 1

1

一件重要的事情是你想使用helper作为一个组件。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

因此,控制器视图组件的助手。

于 2013-07-03T14:26:26.910 回答