Home  >  Article  >  PHP Framework  >  How to implement file upload in laravel

How to implement file upload in laravel

PHPz
PHPzOriginal
2023-04-14 16:19:342413browse

Laravel is a popular PHP framework that is widely used in the development of web applications and websites. In actual application development, file upload is a common requirement. In Laravel, file uploading is very simple and you just need to follow some basic steps.

First, we need to set the relevant configuration of file upload in Laravel. Open the config/filesystems.php file and we can see all the file system configurations in the current system. In this file we can easily add new file system configurations such as local disk, Amazon S3, Rackspace, etc.

Next, we need to create a file upload form so that users can upload files. In Laravel, we can use Laravel Collective’s Form Builder to create forms. This component can easily create HTML form elements, making it simple and easy for us to add file upload controls to the form.

The following is a simple form sample code:

{!! Form::open(['route' => 'upload.file', 'files' => true]) !!}

{!! Form::file('file') !!}

{!! Form::submit('上传文件') !!}

{!! Form::close() !!}

In this example, "route" specifies the request route of the form, and the 'files' parameter tells Laravel that this form needs to upload files. We can use the file() method in Form Builder to create a file upload control.

When the user uploads the file, Laravel will store the file in the "storage/app" directory. To make the uploaded file directly accessible, we need to create a symbolic link.

Open a terminal and enter the following command:

php artisan storage:link

This command will create a symbolic link in the public directory, allowing us to access the uploaded file through a web browser.

Next, we need to process the uploaded file in the controller. Laravel provides a very simple method. We can implement the function of uploading files through the following code:

public function upload(Request $request)
{
    $file = $request->file('file');

    $filename = $file->getClientOriginalName();

    $path = $file->storeAs('upload', $filename);

    return back()->with('success','文件已经上传!');
}

In this code, we first get the uploaded file from the request, and then get the original file name . Next, we use the storeAs() method to store the file in the specified directory and specify the file name. Finally, we use the with() method to set up a success message and then redirect the user back to the original page.

Finally, we need to create a route for the uploaded file to handle the file download function. In the routing file, add the following code:

Route::get('/download/{file}', function ($file) {
    $path = storage_path('app/upload/'.$file);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
})->name('file.download');

In this code, we first get the file path and then check if the file exists. Next, we use the get() method of the File class to read the file content, and use the mimetype() method to obtain the mime type of the file. Finally, we use the Response class to create the response object and send the file contents to the browser.

File uploading is a common requirement, and Laravel provides a very convenient solution. Using Laravel's file upload mechanism, web developers can quickly, easily and securely implement file upload, storage and download functions.

The above is the detailed content of How to implement file upload in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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