Home > Mobile >  Laravel -How to store image file provided by user
Laravel -How to store image file provided by user

Time:12-06

so I have a simple HTML file insert box,

<input id="image" type="file" name="image" value="{{ old('image') }}" accept="image/gif, image/jpeg, image/png" >

but after inserting the image and going to a different page, the website doesn't save the image, which means I cannot use it later. I've looked around for some ways to get around this but I couldn't find anything that worked. is there any way I can store the file? Or another alternative that makes me be able to use them in another page of the website?

If it helps, I have a SQL table called "event" with an attribute image where I'd like to store the link/path to the image provided.

CodePudding user response:

By default, public disks use local drivers and store these files under storage/app/public. To access them via the web, you need to create a symbolic link from public/storage to storage/app/public. Go into the .env file, create a variable called FILESYSTEM_DRIVER, and set it to the public, as shown below:

FILESYSTEM_DRIVER=public

Next, use the command below in order to create the symbolic link.

php artisan storage:link

CodePudding user response:

In order to save the uploaded image and use it later, you will need to store the image on a server and save the location (file path) of the image in your database.

Here is a general overview of the process:

  1. When the user selects an image and submits the form, the PHP script on your server will receive the image file.
  2. You can then use the Laravel File facade's store() method to save the image to a directory on your server. This method returns the file path of the saved image, which you can then save in your database.
  3. To retrieve and display the image on another page of your website, you can use the asset() helper function to generate a URL to the saved image, and then use an tag in your HTML to display the image.

Here is an example of how you might implement this process in your Laravel application:

// In your HTML form:
<form method="post" action="/upload-image" enctype="multipart/form-data">
    <input id="image" type="file" name="image" value="{{ old('image') }}" accept="image/gif, image/jpeg, image/png" >
    <button type="submit">Upload Image</button>
</form>

// In your Laravel route:
Route::post('/upload-image', function (Request $request) {
    // Validate the uploaded image file
    $request->validate([
        'image' => 'required|image|max:2048'
    ]);

    // Store the image on the server and get the file path
    $imagePath = $request->file('image')->store('public/images');

    // Save the image file path in your database
    $event = new Event;
    $event->image = $imagePath;
    $event->save();

    return redirect('/events')->with('success', 'Image uploaded successfully.');
});

// In your HTML to display the image:
<img src="{{ asset('storage/' . $event->image) }}" alt="Event Image">

Note: In this example, I have assumed that you have created an Event model and a corresponding events table in your database, and that you are using the Laravel storage directory to save the uploaded images. You may need to adjust this example to fit your specific needs.

  • Related