faraz73's avatar

uploaded file path is not working

Hi , i have this line of code in my controller

asset(Storage::putFile('public', $request->file('file')));

the returned path result is 404 .. also i have done

php artinsan storage:link

any solutions ?

0 likes
16 replies
Sinnbeck's avatar

Why is it wrapped in asset? That makes little sense

faraz73's avatar

@Sinnbeck controller :

public function store(StoreContactRequest $request)
    {

        $file = Storage::putFile('public', $request->file('file'));

        return "<img src='$file'>";

    }

StoreContactRequest :

public function rules()
    {
        return [
            'fullname' => ['required'],
            'email',
            'phone_number',
            'home_number',
            'file' =>['file','mimetypes:image/jpg,image/png,image/jpeg'],
        ];
    }
Sinnbeck's avatar

@faraz73 Why are you returning a img tag from your store method? Is this used for ajax?

Start by using dd() to see what is stored

        $file = Storage::putFile('public', $request->file('file'));
       dd($file);
faraz73's avatar

@Sinnbeck to see the file path works or not .. the upload has done successfully .. but i don't know how to get correct path to load image in view ...

Sinnbeck's avatar

@faraz73 Normally you would save the path to a model in the database, so you can get it back later on.

faraz73's avatar

@Sinnbeck well ... I should make sure inserted data is correct .. this line

Storage::putFile('public', $request->file('file'));

returns path ... but not works X(

Sinnbeck's avatar

@faraz73 To test it

  1. Upload image and get the path using dd() as explained
  2. Add the path to the url and hit enter
Sinnbeck's avatar

@faraz73 "not working" isnt very informative. What happens? It says the file does not exist?

  1. What is the exact url you are trying with?
  2. What is the path?
  3. Can you see the file in /public/storage
faraz73's avatar
faraz73
OP
Best Answer
Level 4

@Sinnbeck yes .. to get currect path .. i should use :

$file = $request->file('file')->store('public');
$path = Storage::path($file);

this one works ...

Snapey's avatar

what path does it generate and where is the file stored?

Snapey's avatar

based on this "not working" - you don't want to put in the effort then neither do I. It's obviously not that important.

Please or to participate in this conversation.