This tutorial demonstrates how to move files from one folder to another in a Laravel application.
This article provides a few key examples of how to move files in Laravel. These examples will help you move files from one folder to another in Laravel. I'm going to show you how to cut Laravel files from one disk to another.
If you need to move files from one folder to another in your Laravel app, I will guide you on how to do it in Laravel. Laravel provides the File and Storage facades and their methods for working with file systems. I'll provide examples and syntax for both methods so you can use them. You can also learn how to generate fake data using factory models in Laravel with 11 examples.
This solution works on Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9, Laravel 10 and Laravel 11.
Example 1: File Facade Syntax:
<code class="language-php">File::move(from_path, to_path);</code>
Example:
In this example, I have a folder called "exist" in the public folder that contains the test.png image. We move this file to a new folder called "move" and rename it test_move.png. Please take a look at the code below.
<code class="language-php"><?php namespace App\Http\Controllers; use Illuminate\Http\Request; use File; class DemoController extends Controller { /** * 方法构造 * * @return \Illuminate\Http\Response */ public function moveImage(Request $request) { File::move(public_path('exist/test.png'), public_path('move/test_move.png')); dd('文件移动完成。'); } }</code>
Example 2: Storage Facade
Syntax:
<code class="language-php">Storage::move(from_path, to_path);</code>
Example:
In this example, I have a folder called "exist" in the storage folder that contains the test.png image. We move this file to a new folder called "move" and rename it test_move.png. Please take a look at the code below.
<code class="language-php"><?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Storage; class DemoController extends Controller { /** * 方法构造 * * @return \Illuminate\Http\Response */ public function moveImage(Request $request) { Storage::move('exist/test.png', 'move/test_move.png'); dd('文件移动完成。'); }}</code>
Hope this helps.
The above is the detailed content of How To Move File from One Folder to Another In Laravel. For more information, please follow other related articles on the PHP Chinese website!