Home > Backend Development > PHP Tutorial > How To Move File from One Folder to Another In Laravel

How To Move File from One Folder to Another In Laravel

Mary-Kate Olsen
Release: 2025-01-20 14:05:11
Original
300 people have browsed it

How To Move File from One Folder to Another In Laravel

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.

How to move files from one folder to another in Laravel

Example 1: File Facade Syntax:

<code class="language-php">File::move(from_path, to_path);</code>
Copy after login

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>
Copy after login

Example 2: Storage Facade

Syntax:

<code class="language-php">Storage::move(from_path, to_path);</code>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template