skip to Main Content

I use below script to uploade a file to the sevre. it works fine on the local host (wamp server), but when I try to use it on the server I figure out that the uploaded file size is 0 byte.
Any one know taht where is the problem?

public function uploadFile(Request $request)
    {
       
        $request->validate([
            'name' => 'required',
            'auther_id'=>'required',

            'doc_type'=>'required',
            
           'file'=>'required|mimes:pdf'
        ]);
        
        $fileModel = new File;
    
            $fileName = time().'_'.$request->file->getClientOriginalName();
            
            $filePath = $request->file('file')->storeAs('uploads', $fileName, 'public');
          
            $fileModel->name = time().'_'.$request->file->getClientOriginalName();
            $fileModel->file_path = '/storage/' . $filePath;
            $fileModel->save();
             
            
      
    }

2

Answers


  1. Chosen as BEST ANSWER

    Reason of this error was permissions of some folders. I just reset the host completely and now everything work like a charm.


  2. I think you can use like this. first of all you need to store your file inside storage. then you can get public url from storage.

    Try this way

    $fileName = time().'_'.$request->file->getClientOriginalName();
    Storage::put('/public/uploads/'.$fileName,$request->file('file'));
    $url = Storage::url('public/uploads/'.$fileName);
    
    $fileModel = new File;
    $fileModel->name = time().'_'.$request->file->getClientOriginalName();
    $fileModel->file_path = $url;
    $fileModel->save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search