Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DInu98's avatar

After deployment of my project file download doesn't work

on my local machine file download function (ex:- download uploaded image, pdf) work smoothly. but when i published my project, click on download button it will redirect to logout. what is the reason?

controller

?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Course;
use App\Models\material;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Storage;

class AdminCourseMaterial extends Component
{
    use WithFileUploads;

    public $material = [];
    public $month;
    public $course;

    public function render()
    {
        $files = material::paginate(10);
        $courses = Course::all();
        return view('livewire.admin-course-material',[
            'courses'=>$courses,
            'files'=>$files
        ]);
    }

    public function save()
    {
        $this->validate([
            'material.*' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:10000',
            'month'=>'required',
            'course'=>'required'
        ]);

     
        foreach($this->material as $pdf)
        {
            $data = new material();
            $filename = $pdf->getClientOriginalName();
  
            $pdf->storeAs('public',$filename);
            $data->course_id = $this->course;
            $data->file_name = $filename;
            $data->month = $this->month;
            $data->save();
        }
        
      
    }
}

view

<a href="{{ route('admin.file-download',$file->file_name) }}" class="btn bg-indigo"><i class="fas fa-cloud-download-alt mr-2"></i>Download</a>
    

route

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

CourseMaterialsController


    public function download($file_name)
    {
        $path = public_path('storage/'.$file_name);
        return response()->download($path);
        
    }

PreventBackHistory

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class PreventBackHistory
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $headers = [
            'Cache-Control' => 'nocache, no-store, max-age=0, must-revalidate',
            'Pragma','no-cache',
            'Expires','Fri, 01 Jan 1990 00:00:00 GMT',
        ];
        
        return $response;
    }
}

0 likes
21 replies
Nakov's avatar

Make sure you've ran php artisan storage:link on your production server, and check your permissions. Is the file really stored? Check the logs for some errors.

DInu98's avatar

@Nakov yeah i run that command and also file stored on my server

DInu98's avatar

@Nakov

public function download($file_name)
    {
        return Storage::download($file_name);
        
    }

hello @nakov I tried this method but there is an error showing like this, I can't understand what to do :-( on my local machine this works well but after deploy my websit this thing happened

File not found at path: download (5) (2).png
Sinnbeck's avatar

This model name seems wrong. Should be uppercase M, in both filename and classname

use App\Models\material
1 like
Sinnbeck's avatar

How you know it redirects to logout? Because you are signed out or because you can see it url in the network tab?

DInu98's avatar

@Sinnbeck could you please send your email? i will send the admin logins and website

Sinnbeck's avatar

@DInu98 it could a session issue. What driver are you using? Perhaps try another (remeber to run php artisan config:cache to redo the cache afterwards)

1 like
jlrdw's avatar

@DInu98 what system did you develop on, remember Linux is case sensitive.

1 like
DInu98's avatar

@Sinnbeck yeah i run that command you mentioned above but still when i click on download button automatically logout from admin account, also i create function for delete that uploaded file from directory but same thing happen

filesystem.php


    'default' => env('FILESYSTEM_DRIVER', 'local'),

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'public',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],

    ],

  'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

DInu98's avatar

@Sinnbeck this is my session.php

 '  driver' => env('SESSION_DRIVER', 'file'),

    'lifetime' => env('SESSION_LIFETIME', 120),

    'expire_on_close' => false,

    'encrypt' => false,

 '   files' => storage_path('framework/sessions'),

'   connection' => env('SESSION_CONNECTION', null),

   'table' => 'sessions',

'  store' => env('SESSION_STORE', null),

 'lottery' => [2, 100],


    'cookie' => env(
        'SESSION_COOKIE',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),

'path' => '/',

 'domain' => env('SESSION_DOMAIN', null),

 'secure' => env('SESSION_SECURE_COOKIE'),

'http_only' => true,

same_site' => 'lax',

jlrdw's avatar

@DInu98 if your project worked in development, assuming you had laravel setup correctly, meaning:

  • You had pointed to public as document root

It should work the same on a server. What operating system did you use in development?

Have you checked case problems yet?


In windows cASe = case

In linux cASe does not = case

Also test your symlink, can you display images correctly?

1 like
DInu98's avatar

@jlrdw yes symlink working fine. Uploaded images are showing perfect. Im using windows to development can you send a email to [email protected] i will share my livr website and admins logins and source codes then you can identify the problem. This is my first project on live server. Plz help me

jlrdw's avatar

@DInu98 did you double check what @sinnbeck said at:

https://laracasts.com/discuss/channels/laravel/after-deployment-of-my-project-file-download-doesnt-work?page=1&replyId=747523

edit

Also are the files being downloaded private files? If that is the case, you should make a special controller and route for them and use a script. And make sure the person downloading is authorized to receive that download. In such a case, the files need to be out of web folders altogether.

Something like: https://laravel.io/forum/04-23-2015-securing-filesimages

However I modified it to my needs in an app, but it will give you an idea. There are other ways, like using AWS secure storage.

1 like
DInu98's avatar

@jlrdw woow thank you very much this will be really help for me i will follow that steps

Please or to participate in this conversation.