• Feed
  • Explore
  • Ranking
/

    File upload 저장 경로 지정하기

    Laravel
    C
    CodeReaper
    2024.06.18
    ·
    2 min read

    config\filesystem.php 내용 추가

            'custom' => [
                'driver' => 'local',
                'root' => 'D:/Data',
            ],

    이름은 custom으로 지정했고, 호스팅 경로는 C:\xampp\~~~~ 이지만

    데이터는 D:\Data 라는 경로에 저장되도록 하려면 config\filesystem.php를 수정한다.

    결과물은 아래와 같다.

    config\filesystem.php

        'disks' => [
        'custom' => [
            'driver' => 'local',
            'root' => 'D:/Data',
        ],
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],
    
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],</code></pre><p></p><h3>UploadForm.php</h3><pre spellcheck="false"><code class="language-php">&lt;?phpnamespace App\Livewire;use Livewire\Attributes\Rule;
    use Livewire\Component;
    use Livewire\WithFileUploads;class UploadForm extends Component
    {
    use WithFileUploads;#[Rule('required|file|max:102400')] // 1GB Max
    public $attachFile;
    
    public function uploadFile()
    {
        session()-&gt;flash('message', 'File uploaded successfully: ');
        $this-&gt;validate();
    
        // 'custom'으로 맵핑된 경로에 'attachFile'이라는 폴더에 저장한다. (없으면 자동 생성)
        $path = $this-&gt;attachFile-&gt;store('attachFile', 'custom');
    }
    
    public function render()
    {
        return view('livewire.upload-form');
    }}

    아래 명령을 통해 config\livewire.php 파일 생성

    php artisan livewire:publish --config

    livewire 설정에서 disk를 아까 설정한 custom으로 지정해주고 rules를 적절히 설정해준다.

    
        'temporary_file_upload' => [
            'disk' => 'custom',        // Example: 'local', 's3'              | Default: 'default'
            'rules' => 'file|max:1048576',       // Example: ['file', 'mimes:png,jpg']  | Default: ['required', 'file', 'max:12288'] (12MB)
            'directory' => null,   // Example: 'tmp'                      | Default: 'livewire-tmp'
            'middleware' => null,  // Example: 'throttle:5,1'             | Default: 'throttle:60,1'
            'preview_mimes' => [   // Supported file types for temporary pre-signed file URLs...
                'png', 'gif', 'bmp', 'svg', 'wav', 'mp4',
                'mov', 'avi', 'wmv', 'mp3', 'm4a',
                'jpg', 'jpeg', 'mpga', 'webp', 'wma',
            ],
            'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated...
            'cleanup' => true, // Should cleanup temporary uploads older than 24 hrs...
        ],