DInu98's avatar

How to download uploaded file

I tried to upload file and download it but when I click my download button there is an error like this,

The file "E:\English HUB\English_HUB\storage\uploads/1633978895_කර්මාන්ත රසායනය.pdf" does not 
exist"

this is my controller

 public function store(Request $request)
    {
        $validator = $request->validate([
            'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:10000',
            'course'=>'required'
        ]);
    
            $file = new material();
    
            if($request->file()) {
                $name = time().'_'.$request->file->getClientOriginalName();
                $filePath = $request->file('file')->storeAs('uploads', $name, 'public');
    
                $file->file_name = time().'_'.$request->file->getClientOriginalName();
                
                $genid = (string)Uuid::generate();
                $file->uuid = $genid;
                $file->file_path = $filePath;
                $file->course_id = $request->course;
                $file->save();
    
                return back()
                ->with('success','File has uploaded to the database.')
                ->with('file', $name);
            }
    }

download method

public function download($uuid)
    {
        $file = material::where('uuid',$uuid)->first();
        $pathofFile = storage_path($file->file_path);
        return response()->download($pathofFile);
    }

this is my route

 Route::get('/course-material/{uuid}/download',[CourseMaterialsController::class,'download'])->name('file-download');

my upload pdf files save to this path in my project "E:\English HUB\English_HUB\public\storage\uploads"

0 likes
13 replies
lbecket's avatar

@DInu98 Maybe a silly question, but did you verify that the file exists on the server and in the expected location?

lbecket's avatar

@DInu98 the next thing that I would try would be to dd($pathofFile) to confirm that the path you're referencing matches the path that you think it should. I'd also plug the dumped path directly into my browser to confirm that it downloads the file.

I noticed that the file path you listed has some non-English characters. Perhaps that's creating an issue.

1 like
Snapey's avatar

you are saving it to the public disk but you are not retrieving it from the public disk

1 like
DInu98's avatar

@Snapey yeah.. could you please tell me how to retrieve it from publicl disk

DInu98's avatar

@Snapey @lbecket this is the way i used to retrieve from public

  public function download($uuid)
    {
        $file = material::where('uuid',$uuid)->first();
        $pathofFile = public_path("storage/'$file->file_path");
        return response()->download($pathofFile);
    }

dd($pathofFile);

"E:\English HUB\English_HUB\storage\uploads/1634025696_Online Assignement 2021 -IT2030 - June Intake.pdf"
Snapey's avatar

compare exactly. Is that the location of your file?

DInu98's avatar

@Snapey hi, my upload file location is English_HUB/Public/storage/uploads i have run the command php artisan Storage:link

also these same files are upload into this path English_HUB/storage/app/public/uploads

lbecket's avatar

@DInu98 if the file exists here English_HUB/storage/app/public/uploads then the symlink should allow you to access it here English_HUB/storage/uploads

You ran the artisan command, but I would confirm that it actually created the symlink and I would also check the permissions of the file to confirm that it's readable to a web user.

1 like
DInu98's avatar

@lbecket do you have any tutorials or some articles about this? then I can start from beginning this upload and download method

Snapey's avatar
Snapey
Best Answer
Level 122

You are trying to download the file to the client, so the file does not need to be in the public folder. Whether you have the symlink or not is irrelevant in this case.

You have the location where the file is

You have the path that you are trying to give to the download function. THEY MUST BE THE SAME.

Debug the path

  public function download($uuid)
    {
        $file = material::where('uuid',$uuid)->first();
        $pathofFile = public_path("storage/'$file->file_path");

	dd($pathofFile);

        return response()->download($pathofFile);
    }
2 likes

Please or to participate in this conversation.