Cakephp上传插件 - 定义保存文件的目录


cakephp upload plugin - define what directory to save files in

我正在使用Jose Gonzalez的cakephp-upload插件将图像上传到我们的应用程序。默认情况下,它们保存在如下目录中:webroot/files/user/photo/{user_id}这很好,除了当我们想使用 $this->Html->image() 在 webroot/img 目录中搜索图像时显示此类图像时。

我已经尝试显示图像

echo $this->Html->image('../files/user/photo/' .
        $user['User']['photo_dir'] . '/' .
        $user['User']['photo']);

这有效,但我想知道是否有某种方法可以告诉这个插件保存到 img 目录中?文档没有提到任何这些。

而且,有没有办法告诉$this->Form->input('User.photo', array('type' => 'file'));只接受图像文件?

正如

你在这个文件中看到的,path设置如下:

public $defaults = array(
    'rootDir' => null,
    'pathMethod' => 'primaryKey',
    'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}',
...

您可以将其更改为:

'path' => '{ROOT}webroot{DS}img{DS}'

对于第二个问题,您可以使用accept属性,例如:

$this->Form->input('User.photo', 
    array(
        'type' => 'file', 
        'options' => array('accept' => 'image/*')
    )
);