cakePHP从函数/方法中分配var



下面是我的代码:

class ManImportsController extends AppController {
    public $uppath;
    var $name       = 'ManImports';
    var $uses       = array ('RFupload','ManImport');
    var $helpers    = array ('Html', 'Session','Time', 'Form', 'Js', 'Javascript','DatePicker','Ajax','IrDependentArray','databaseFields','FileUpload.FileUpload', 'showFields');
    var $components = array ('FileUpload.FileUpload','Session','RequestHandler');
    var $actsAs     = array('FileUpload.FileUpload' => array(
                      'uploadDir' => $this->uppath,           // Primary Upload Path
                      'forceWebroot' => false,                // false, files upload to uploadDir
                      'fields' => array ('name' => 'name', 'size' => 'size',
                      'date' => 'date', 'created' => 'created',
                      'type' => 'type'),
                      'allowedTypes' => array ('csv' => array('application/csv'),
                      'xls' =>array('application/vnd.ms-excel'),
                      'xlsx' =>array('application/vnd.ms-excel')),
                      'required' => false,                    // true = errors when file isn't uploaded.
                      'maxFileSize' => false,                 // false to turns off maxFileSize
                      'unique' => true,                       // true will overwrite files with same name.
                      'massSave' => true,
                      'fileNameFunction' => false));          //execute the Sha1 function on a filename before saving it (default false)
    public function beforeFilter() {
       $this->uppath = $this->get_path();
    }  // end function beforeFilter
    function get_path() {
       $mach = gethostname();
       if ($mach=='my_machine_ID') {
          $path = "C:/home/files/uploads/";
       } else {
          $path = '/home/files/uploads/';
       }  // end if $mach
       return $path;
    }  // end function get_path
} // end class ManImportsController

问题是通过正确调用函数/方法"get_path"来正确分配"uploaddr"。我已经尝试了大约4种不同的组合,从cakePHP约定和PHP OOP,但到目前为止没有任何工作。

你知道我在这里遗漏了什么吗?你能告诉我正确的调用方法吗?

我必须在Linux上部署,但需要目前与Win7开发机一起工作。

首先,$actsAs用于指定模型行为。你永远不应该在你的控制器中使用$actsAs。

仔细阅读说明书——你要么:
a)在你的模型中通过$actsAs实现行为(在这种情况下,你根本不需要控制器中的组件)或
b)如果你真的不需要模型,而是想要实现组件,那么你就把组件包含在你的控制器中,你根本不需要$actsAs。(注意,这是不推荐的方式)

但是不管怎样,你都不应该在你的控制器中使用$actsAs。

编辑:顺便说一下,我显然不知道你的具体要求,或者你使用的是什么版本的CakePHP,但是这个上传插件比你使用的那个更新和积极维护得要快得多。

最新更新