CakePHP媒体插件-上传文件夹

CakePHP媒体插件-上传文件夹,cakephp,plugins,cakephp-1.3,Cakephp,Plugins,Cakephp 1.3,我正在使用媒体插件(http://www.ohloh.net/p/cakephp-media) 我想为所有上传定义自定义文件夹。我有点不明白该在哪里做。 这是我想要实现的文件夹结构 webroot/media/image/original (for the original file storage) webroot/media/image/large (for the large image filter) webroot/media/image/medium (for the medium

我正在使用媒体插件(http://www.ohloh.net/p/cakephp-media)

我想为所有上传定义自定义文件夹。我有点不明白该在哪里做。 这是我想要实现的文件夹结构

webroot/media/image/original (for the original file storage)
webroot/media/image/large (for the large image filter)
webroot/media/image/medium (for the medium image filter)
webroot/media/image/small (for the small image filter)
另外,我想使用一个随机名称,我想使用以下sript生成该名称

//UUID generator
function _imgName() {
    return time() . substr(md5(microtime()), 0, 12);
}

首先,使用了这个插件后,我建议考虑默认的目录结构,以使你的应用程序更简单。你不想直接编辑插件,因为它会让升级更痛苦

# /app/webroot/media/transfer/img/slug.ext (for the original file storage)
# /app/webroot/media/filter/l/img/slug.ext (for the large image filter)
# /app/webroot/media/filter/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/filter/s/img/slug.ext (for the small image filter)
但是,媒体插件配置文件位于
/app/plugins/Media/config/core.php
,其中包含一些常量,您可以先在
/app/config/bootstrap.php
中定义这些常量来覆盖整个应用程序。要实现与所需格式类似的格式,可以定义以下变量:

define('MEDIA_TRANSFER', WWW_ROOT . 'media' . DS . 'original' . DS);
define('MEDIA_FILTER', WWW_ROOT . 'media' . DS);
define('MEDIA_TRANSFER_URL', 'media/original/');
define('MEDIA_FILTER_URL', 'media/');

# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/l/img/slug.ext (for the large image filter)
# /app/webroot/media/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/s/img/slug.ext (for the small image filter)
(注意:在将行为添加到模型时,通过传递正确的配置选项,还可以根据每个模型设置上述路径。)

您还可以重新定义用于更接近目标的图像过滤器的名称。您需要在
/app/config/bootstrap.php中再次执行此操作,但在加载媒体插件配置后:

require APP . 'plugins' . DS . 'media' . DS . 'config' . DS . 'core.php';
Configure::write('Media.filter.image', array(
    'small' => array('convert' => 'image/png', 'zoomCrop' => array(100, 100)),
    'medium' => array('convert' => 'image/png', 'fitCrop' => array(300, 300)),
    'large' => array('convert' => 'image/png', 'fit' => array(600, 440)),
));

# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/large/img/slug.ext (for the large image filter)
# /app/webroot/media/medium/img/slug.ext (for the medium image filter)
# /app/webroot/media/small/img/slug.ext (for the small image filter)
如果您阅读了的文档,您将看到您可以通过在模型中重新实现此方法(例如,
MyModel::transferTo()
)来自定义路径的后半部分(即
img/slug.ext
)。像这样的事情会让你更接近:

class MyModel extends AppModel {
    public function transferTo($via, $from) {
        extract($from);
        $mime = Mime_Type::guessName($mimeType ? $mimeType : $file);
        $name = $this->_imgName();
        $path  = $mime . DS . $name
        $path .= !empty($extension) ? '.' . strtolower($extension) : null;
        return $path;
    }
}

# /app/webroot/media/original/image/129916996632c787226a0b.ext (for the original file storage)
# /app/webroot/media/large/image/129916998392a3570a1828.ext (for the large image filter)
# /app/webroot/media/medium/image/12991699891c7625bebedb.ext (for the medium image filter)
# /app/webroot/media/small/image/12991699938ab22d80cfc6.ext (for the small image filter)

虽然不完全是您想要的(
/large/image/
vs
/image/large/
),但这是您不需要重新实现插件的大部分就可以做到的。这是因为路径被视为两个部分(例如
/media/large/
image/129916998392a3570a1828.ext
),它们随后被附加在一起。您可以在和中看到发生的追加操作。您需要扩展插件并在应用程序代码中复制这些方法(进行修改),或者直接破解这两行代码以获得所需的输出

哇。非常感谢戴泽尔的回答。我只是希望路径可以在不进行黑客攻击的情况下更改:)我收到此错误生成器行为::make-目录`C:\wamp\www\test\app\webroot\media\large\C:\wamp\www\test\app\webroot\img`无法创建或无法写入。请检查权限。路径也不是我想要的:(