How to Move Files in Laravel Using File and Storage Facades

This tutorial explains how to move files in a Laravel application, showing syntax and complete code examples for both the File and Storage facades, compatible with Laravel versions 5 through 11, and includes guidance on ensuring target directories exist.

php Courses
php Courses
php Courses
How to Move Files in Laravel Using File and Storage Facades

This tutorial demonstrates how to move files within a Laravel application, providing key examples for moving files between folders and across disks using Laravel's File and Storage facades, compatible with Laravel versions 5 through 11.

Method 1: Using the File Facade

Syntax:

File::move(from_path, to_path);

Example:

Assuming a folder exist under the public directory contains test.png, the file is moved to a new folder move and renamed to test_move.png.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;

class DemoController extends Controller
{
    public function moveImage(Request $request)
    {
        File::move(public_path('exist/test.png'), public_path('move/test_move.png'));
        dd('File moved successfully');
    }
}

Method 2: Using the Storage Facade

Syntax:

Storage::move(from_path, to_path);

Example:

Assuming a file test.png exists in storage/app/exist, it is moved to storage/app/move and renamed to test_move.png.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Storage;

class DemoController extends Controller
{
    public function moveImage(Request $request)
    {
        Storage::move('exist/test.png', 'move/test_move.png');
        dd('File moved successfully');
    }
}

Ensure the target directories exist before attempting to move files.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPfile systemLaravel
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.