dzthe's avatar
Level 1

How to find the correct path to download file in laravel

the file store perfectly into my storage but in my show page i can download the file but it show - failed - no file <- so i assumed that my path is wrong but i cant figure it out. can somebody help me. OR did i write a wrong coding? thanks

http://imgur.com/HJkXVZR <-- this picture is my public folder.

http://imgur.com/DciV8Wv <-- my storage file

i've link this file to public folder.

and this is my coding for show page which the user can download the file from the browser.


  <div class="row">
                  <div class="col-lg-12">
                      <section class="panel">
                          <center><header class="panel-heading">
                              <centre>test example</centre>
                          </header></center>

                           <table class="table table-striped table-advance table-hover">
                           <tbody>
                              <tr>
                                 <th><i class="icon_profile"></i> writer name</th>
                                
                                 <th><i class="icon_pin_alt"></i>file upload</th>
                                 <th><i class="icon_pin_alt"></i>Action</th>

                              </tr>
                             
                                  @foreach($example as $user1)
                                    <tr>
                                    <td>{{$user1->name}}</td>
                                    
                                     <td>{{$user1->upload}}</td>
                                     <td>

                                   <a href="MMSProject/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">
                                  
                                     <button type="button" class="btn btn-primary">Download</button></a></td>
                                    </tr>
                                    
                                  @endforeach
                                  
                                  
                                  

                              </tbody>
                              </table>



                      </section>
                  </div>
     </div>

0 likes
21 replies
mushood's avatar

You can proceed this way to find the correct path.

<a href="{{ base_path() . "/public/storage/app/public/upload/".$user1->upload}}>

From documentation: The base_path function returns the fully qualified path to the project root.

Link: https://laravel.com/docs/5.4/helpers#method-base-path

You could also use public_path()

Then it would look like this:

<a href="{{ public_path() . "/storage/app/public/upload/".$user1->upload}}>

The public_path function returns the fully qualified path to the public directory.

However, if you followed the instructions from here: https://laravel.com/docs/5.4/filesystem

you can load it in this way:

<a href="{{ asset("storage/app/public/upload/".$user1->upload) }}>

Hope this helps

Hujjat's avatar

Can you share your controller code? Do you move the uploaded files to the public folder?

dzthe's avatar
Level 1

@Hujjat

 public function store(Request $request)
    {
        
        //create a new post using the request data
        $user1 = new user1;
        $user1->name= request('name');
       // $user1->upload= request('upload');
        if ($request->hasFile('upload')) {
            $filename = $request->upload->getClientOriginalName();
            $request->upload->storeAs('public/upload',$filename);
            $user1->upload=$filename;

           
        

        //save it into database
        $user1->save();

        //and then return to the homepage.

        return view('user.example');

        $rules=array(
            'name' => 'required',
            'upload'=> 'required|mimes:doc,docx,jpeg,jpg,png');
        $validator = validator::make(request::all(),$rules);
        if ($validator->fails()) {

            $messsage=$validator->messages();

            return Redirect::to('user.profile')->withErrors($validator);
            
        }else if($validator->passes()){
            echo "succes validator";

        }
    }

i alredy link the storage file

dzthe's avatar
Level 1

@mushood

<a href="{{ base_path() . "/public/storage/app/public/upload/".$user1->upload}}>

i use this coding , but my download button did not work? or do i have to change my other coding too if i follow coding above? im sorry still new to laravel.

mushood's avatar

@dzthe Could you try the other two as well?

Also, when the page loads in browser, try to inspect the button. (right-click -> inspect). You will see the link. Copy paste the link directly in your browser to see if you can see the file. If it does not work, please post the link that you saw.

Don't worry. We were all new at some point. I guess i consider myself new too. People on laracast are always welcoming, dont feel shy to ask. NOT asking questions is the only wrong option.

Hujjat's avatar

You can move the file while you upload it. I mostly use this technique

`

 if ($request->upload !=  "" ) {

        $fileName = time().'.'.$request->upload->getClientOriginalExtension();
        $request->upload->move(public_path('FileFolder'), $fileName );

    }

  $user1->upload = $fileName; 

`

Now you can use the following in your view

<a href="{{ asset('FileFolder') }} / {{$user1->upload}}> Download </a>

I haven't tested this code. but I hope you got the idea.

dzthe's avatar
Level 1

@mushood i did try the third one

<a href="{{ asset("storage/app/public/upload/".$user1->upload) }}>

and the button is clickable but i got error said " NotFoundHttpException in RouteCollection.php line 179: " and the url was something like this "http://127.0.0.1:8000/storage/app/public/upload/3.JPG%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cbutton%20type="

btw i did not make any coding changes except for this only

<a href="{{ asset("storage/app/public/upload/".$user1->upload) }}>

or if i follow this coding do i have to make changes on other coding too? like controller or something ???

i tried this one too

<a href="{{ base_path() . "/public/storage/app/public/upload/".$user1->upload}}>

the button are still not clickable so i do as you said. inspect the button here is the link " http://127.0.0.1:8000/example/C:\MMSProject/public/storage/app/public/upload/3.JPG "

and i got the same error too "NotFoundHttpException in RouteCollection.php line 179:" i dont know did i miss some coding ?

dzthe's avatar
Level 1

@mushood i got the same error "NotFoundHttpException in RouteCollection.php line 179: "

what route should i create???

mushood's avatar

@dzthe To access files in your public folder, you dont need to set any route.

From what you are telling me, http://127.0.0.1:8000/example/C:\MMSProject is equal to base_path() and this should not be correct.

I'll keep thinking but I am also stuck.

Edit: I dont understand where "C:\MMSProject" came from

dzthe's avatar
Level 1

@mushood im sorry. is okay tho. i've been stuck on this problem for 4 days already. and i dont really know how to continue or how to correct the problem or fix the coding. been searching for the answer quite few days aready and still stuck on the same problem lol. btw thank you

dzthe's avatar
Level 1

@Hujjat i got this error " NotFoundHttpException in RouteCollection.php line 179: "

mushood's avatar

@dzthe

Still wondering

can you try this and let me know what link you have:

<a href="{{ "This is base path: " . base_path() . "this is public path" . public_path() }}>

let me know what is the link you see when you inspect.

dzthe's avatar
Level 1

@mushood do you need to see other coding ? MMSProject is my project folder name. its probably because i store those upload file into the that folder. i have the uploaded file in this file c:mmsproject/storage/app/public/upload im sorry if this can relate or not

dzthe's avatar
Level 1

@mushood i got this link " http://127.0.0.1:8000/example/This%20is%20base%20path:%20C:/MMSProjectthis%20is%20public%20pathC:/MMSProject/public%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cbutton%20type= "

when i inspect i got this " public%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20…%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cbutton%20type= Failed to load resource: the server responded with a status of 404 (Not Found) "

mushood's avatar

@dzthe

okay. So your file should be:

<a href="C:/MMSProject/storage/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">

OR (assuming symlink)

<a href="C:/MMSProject/public/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">
dzthe's avatar
Level 1

@mushood i tried the first one but idk if this is actually working or not because i got this http://imgur.com/UuNrHu4 and it has been more than 10 minutes for it to show up the download popup. when i check my chrome download history the file did not show up which understandable because the download popup did not show up yet and then i try to close the window it said http://imgur.com/LWqno7K

probably my internet connection ?

mushood's avatar

@dzthe

Alright, the issue should not have shifted from the path to the download, but just to be sure.

if you put this link directly in the browser, do you see the image?

Link: C:/MMSProject/public/storage/app/public/upload/3.JPG

Also i made a mistake in the first one. Here is the correct version:

<a href="C:/MMSProject/public/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">

I put storage twice in the previous link.

dzthe's avatar
Level 1

@mushood when i use this link " Link: C:/MMSProject/public/storage/app/public/upload/3.JPG" it said Your file was not found

It may have been moved or deleted. ERR_FILE_NOT_FOUND"

so i tried to remove the public words using this link "file:///C:/MMSProject/storage/app/public/upload/3.JPG" and the picture do show up.

<a href="C:/MMSProject/public/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">
<a href="C:/MMSProject/storage/app/public/upload/{{$user1->upload}}" download="{{$user1->upload}}">

i've tried both .

but then i again i get the same problem the download popup did not show up like i've mention above :(

dzthe's avatar
Level 1

@mushood so apparenlty i open the main public folder and i got this url by opening the image with chrome " file:///C:/MMSProject/public/storage/upload/3.JPG " and yeah i try type the url and the picture show up

but still the same thing happen the download popup did not show up , so dont know whether this coding is actually working or not.

Please or to participate in this conversation.