Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在CakePHP中获取基本目录_Cakephp_Absolute Path_Base Path - Fatal编程技术网

在CakePHP中获取基本目录

在CakePHP中获取基本目录,cakephp,absolute-path,base-path,Cakephp,Absolute Path,Base Path,我正在从事一个CakePHP项目。在这里,我必须上传一个文件,然后将该文件移动到一个特定的文件夹。上传工作正常,但我无法将其移动到我想要的文件夹。CakePHP中是否有任何函数或东西可以移动上传的文件 目前我正在使用PHP的核心函数move\u uploaded\u file()。此函数需要源和目标的绝对路径,而不是http://localhost/....,我试过了,但没用。那么,您知道如何获取应用程序的基本目录/绝对路径吗?这就像:C://wamp/www/project\u folder/

我正在从事一个CakePHP项目。在这里,我必须上传一个文件,然后将该文件移动到一个特定的文件夹。上传工作正常,但我无法将其移动到我想要的文件夹。CakePHP中是否有任何函数或东西可以移动上传的文件


目前我正在使用PHP的核心函数
move\u uploaded\u file()
。此函数需要源和目标的绝对路径,而不是
http://localhost/....
,我试过了,但没用。那么,您知道如何获取应用程序的
基本目录/绝对路径吗?这就像:
C://wamp/www/project\u folder/…

蛋糕生成(或交给您…)的常量记录在中


使用Cake的全局
APP
似乎是一个不错的选择(除非您不想移动
APP/
文件夹中的文件)。

您可以使用php函数获取应用程序的基本目录。

在Controller中添加函数

public function add() {
    $this->Model->create();
    if ($this->request->is('post')) {
        if(!empty($this->data['Model']['image']['name']))
        {
        $file=$this->data['Model']['image'];
        $ary_ext=array('jpg','jpeg','gif','png');
        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
            if(in_array($ext, $ary_ext))
            {
                move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . $file['name']);
                $this->request->data['Model']['image'] = $file['name'];
            }   
        }
        if ($this->Model->save($this->request->data)) 
        {
            $this->Session->setFlash('Your record has been saved.');
            $this->redirect(array('action' => 'index'));
        }
        else 
        {
            $this->Session->setFlash('Unable to add your record.');
        }
    }
}


File control in add.ctp file

echo $this->Form->input('image',array('type'=>'file'));


you can define constant in index.php file like below
/**
 *  Get Cake's root directory
 */
define('APP_DIR', 'app');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
define('WEBROOT_DIR', 'webroot');
define('WWW_ROOT', ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS);

是的,这个常数似乎对我有用。但是,如何使用常数呢$这个->应用程序?@JorickLNo,只需将应用程序放在PHP代码中的某个地方。它会自动转换$var=APP。D你的文件夹';还有,“DS”是否适用于所有类型的服务器?我的意思是,在本地主机中,我需要“/”,在服务器中需要“\”。那么,它是否在所有平台上都提供了正确的字符,或者,我必须为此配置它?@JoricklAs书中说:>PHP的DIRECTORY_SEPARATOR的缩写,在Linux和windows上是/的缩写。你为什么不试试,在你的某个观点中重复一下呢?如果你
他们,你会发现你刚刚创建了什么URL…;)是的,我正在使用它。@jorickly是的,这个函数很好,我正在使用它。但我需要一个更好的方法@菲拉夫