0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

プラグイン「UploadPack」の保存場所をwebroot以上にする

Last updated at Posted at 2016-07-11

デフォルトだとwebrootの下のフォルダにファイルが保存されてしまうので、
pathを変更してwebrootより上の階層にファイルを保存したい。

やりたいことは簡単。pathをwebrootより上に指定すればいい。

https://github.com/szajbus/uploadpack#you-can-change-the-path-where-the-files-are-saved
上記URLを参考にCakePHP/dataにファイルを保存すべく設定してみる。

var $actsAs = array(
    'UploadPack.Upload' => array(
        'item' => array(
            'path' => ':app/../data/img/:basename_:style.:extension'
        )
    )
);

:appはappディレクトリのこと。CakePHPはそれより上なので../を指定。
これで任意の場所に保存ができる。

ただし、これだけだと保存場所だけが変わってヘルパーとの連携がとれない。
path変更のところを読み進めていくとなにやらYou can change the url the helper points to when displaying or linking to uploaded filesと書いてある。
なんだか怪しい。

var $actsAs = array(
    'UploadPack.Upload' => array(
        'item' => array(
            'url'  => '/view(任意のController名)/image(任意のアクション)/:id/:basename_:style.:extension',
            'path' => ':app/../data/img/:id/:basename_:style.:extension'
        )
    )
);

こんな感じにしてみる。
それからViewでドキュメント通り表示してみると↓

echo $this->Upload->uploadImage($image['Image'], 'Image.item')

HTMLが↓のように生成される。

<img alt="" src="/view/image/6/test.png">

あとは、/view/imageに引数を与えてアクセスした時に保存した場所から画像を出力できるようにすればいい。

画像の表示についてはこちら

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?