John Davidson

php - How can i access a file in local?

0 comments
Message:


I am trying to get an image that is uploaded from the user.


Upload function:


public function updatedUpload($upload){
$object = $this->currentTeam->objects()->make(['parent_id' => $this->object->id]);
$object->objectable()->associate(
$this->currentTeam->files()->create([
'name' => $upload->getClientOriginalName(),
'size' => $upload->getSize(),
'path' => $upload->storePublicly('files', ['disk' => 'local'])
])
);

$object->save();
$this->object = $this->object->fresh();
}

This gets me this link in the database:


files/9yzPCLZzlT2aiogc8A8DQIdJTNrkiZ0eu0QTESFF.jpg

How can i access this through an image so i can see the picture?



My Answer:
You can access the file by creating a route in your PHP application that serves the file. Here's an example of how you can do this:

1. Create a route in your routes file (e.g. web.php) that serves the file:

php
Route::get('/files/{filename}', function ($filename) {
$path = storage_path('app/public/' . $filename);

if (!file_exists($path)) {
abort(404);
}

return response()->file($path);
});


2. In your view file, you can display the image using the URL generated by the route:

html
Image


Make sure to replace `$object->file->path` with the actual path to the file in your database.

This way, when you access the URL generated by the route, it will serve the file and display the image in the browser.

Rate this post

5 of 5 based on 5201 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us