将文件上传到日期文件夹之前的自定义位置 [英] Upload Files to custom location before date folder

查看:128
本文介绍了将文件上传到日期文件夹之前的自定义位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bolt 2.0中使用图像字段构建了一个自定义内容类型。

I have built a custom contenttype with an image field in Bolt 2.0.

    image:
        type: image

如果未指定文件夹,则上传的文件将转到以年份-月份命名的文件夹

If no folder is specified the uploaded file goes to a folder named by the year-month

结果:2014-11 /myFileName.jpg

Result: 2014-11/myFileName.jpg

通过标签上传,我可以将其更改为其他内容。

With the tag upload I can change this to something else.

    image:
        type: image
        upload: "News/" 

结果:News / myFileName.jpg

Result: News/myFileName.jpg

是否有可能在我的costom路径之后获取年月文件夹?

Is it possible to get the year-month folders after my costom path?

结果:新闻/2014-11/myFileName.jpg

Result: News/2014-11/myFileName.jpg

推荐答案

答案是肯定的,但不是非常简单,因此,如果您想要一种可配置的方式来执行此操作,则需要等待2.1的Bolt,我们将在其中添加变量至 upload:设置。

The answer to this is yes, but not very simply so if you want a configurable way to do this you need to wait for 2.1 of Bolt where we're going to add variables to the upload: setting.

如果您不介意设置自己的引导文件并修改应用程序,则可以立即进行操作。

If you don't mind setting up your own bootstrap file and modifying the application then you can do it now.

日期前缀由$ app [‘upload.prefix’]设置生成,当前返回日期字符串。您需要做的就是修改它,将其更改为您自己的闭包。我尚未在项目中测试过此项目,因此可以根据需要进行调整,但需要经过以下步骤:

The date prefix is generated by the $app['upload.prefix'] setting and currently returns the date string. What you need to do to modify this is change this to your own closure. I haven't tested this on a project so tweak if needed but after:

$app->initialize();

// Redefine the closure
$app['upload.prefix'] = function() {
    $setting = $app['request']->get('handler');
    $parts = explode('://', $setting);
    $prefix = rtrim($parts[0], '/') . '/';
    return $prefix.date('Y-m') . '/';

};
$app->run();

我们在这里所做的是读取请求中传递的设置,然后串联默认值日期前缀放在其末尾。

What we're doing here is reading the setting which is passed in the request and then concatenating the default date prefix onto the end of it.

如前所述,2.1将在路径中引入变量支持,因此类似

As mentioned earlier 2.1 will see variable support introduced into the paths so options like

 upload: news/{%month%}/{%day%}
 upload: uploads/{%contenttype%}/{%id%}

很容易在contenttypes.yml文件中定义,因此,如果您不介意等待几个月的时间,显然要简单得多。

will be easily definable in the contenttypes.yml file so If you don't mind waiting for a couple of months then this is obviously much simpler.

这篇关于将文件上传到日期文件夹之前的自定义位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆