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 1 year has passed since last update.

アップロードされたファイルをdockerに保存する

Last updated at Posted at 2023-10-28

業務で初めて行ったので備忘録
誤字、他にいいやり方あれば指摘お願いします

前提

Routes、Controllerは作成済み、Serviceから書きます

1.storageに保存する

requestからファイルを取得する場合はfile()を使用する
file()メソッドの引数にinput要素のname属性を指定することでオブジェクトを得られる
ファイル名を取得したい場合はgetClientOriginalName();など便利なメソッドがいくつかあります
参考サイトはこちら

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;


class TestService 
{
    public function create(Request $request)
    {
        $indexHtml = $request->file('index_html');
        $mainImage = $request->file('main_image');
        $mainImageName = $mainImage->getClientOriginalName();
    }

ファイルをstorageに保存してくれるstore()storeAs()メソッドを使用する

store()

store('ディレクトリ名','保存ファイル);で指定が可能
store('')とディレクトリ名を空にするとstorage/appに保存される
store('test')にするとstore/app/testにファイルが保存される

$取得したファイル->store('ディレクトリ名')でも可能

storeAs()

'storeAs('ディレクトリ名','保存ファイル',ファイル名')`でも指定が可能
ファイル名をつけたい時にstoreAsで保存すればいいそうです

$取得したファイル->storeAs('ディレクトリ名','ファイル名')でも可能

public function create(Request $request)
    {
        $indexHtml = $request->file('index_html');
        $mainImage = $request->file('main_image');
        $mainImageName = $mainImage->getClientOriginalName();

        if(isset($indexHtml)) {
            $indexHtml->storeAs('test','index.html');
        }
        if(isset($mainImage)) {
            $mainImage->storeAs('test',$mainImageName);
        }
    }

2.dockerコンテナにコピーする

コピーはFile::copyDirectory('対象ディレクトリ','移動先)を使用する
storage_pathは/var/www/.../storageになる

Illuminate\Support\Facades\File
今回はvolumeにコピーします

$baseDirectory = storage_path('app/test');
$moveDirectory = '/mnt/ボリューム名/test';

File::copyDirectory($baseDirectory,$moveDirectory);

//今回はstorage/app/testの方はコピーが完了すれば不要なので削除します
Storage::deleteDirectory('test');

(補足)権限がないとエラーが出る

"copy(/mnt/www_data_store/index.html): Failed to open stream: Permission denied",とエラーが出るのでこの場合はコンテナの権限を変更する

ローカル環境ではなくコンテナに入って実行する

$ docker exec -it コンテナ名 bash
$ cd mnt
$ ls la
>>drwxr-xr-x ....  ボリューム名     //現在の権限、これではコピーできないので変更する
$ chmod -R 777 /mnt/ボリューム名/

毎回docker exec -it コンテナ名 bashを実行しなくても
vscodeの「Remote Explorer」という拡張機能を使うと便利

追記

storageに一度保存せずそのままdockerに保存出来る方法を知ったので記録しておきます

アップロードされた画像に関しては今回は「main_image」にリネームして拡張子はアップロードされた画像に合わせて保存するようにしています。
例).pngでアップロードされた場合→「main_image.png」
.jpgでアップロードされた場合→「main_image.jpg」

use Illuminate\Support\Facades\File;


$moveDirectory = '/mnt/ボリューム名/test/';

if (!file_exists($moveDirectory)) {
    File::makeDirectory($moveDirectory);
}

if (isset($_FILES['input_html'])) {
    File::move($_FILES['input_html']['tmp_name'],$moveDirectory.'index.html');
} 
if (isset($_FILES['main_image'])) {
    $extension = pathinfo($_FILES['main_image']['name'], PATHINFO_EXTENSION);
    File::move($_FILES['main_image']['tmp_name'],$targetDirectory.'main_image.'.$extension);
}

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?