saving file in a specific user folder and show it

Posted 1 year ago by jahstation

I've a strange problem on my saveFile() method:

public function saveFile(Request $request){
        $username=Auth::user()->name;
        if ( preg_match('/\s/', $username) ){
            $username= preg_replace('/\s+/','', $username);
        }
        if(! Storage::disk('public')->has("users/".$username."_".Auth::user()->id)){
            Storage::makeDirectory("public/users/".$username."_".Auth::user()->id);
        }

        $patz=asset(Storage::url("users/".$username."_".Auth::user()->id));
        $now= new \Carbon\Carbon();
        $fileName = 'ddt_'.$username."_".$now->getTimestamp().'.'.$request->ddt_file->getClientOriginalExtension();

        $pathComplete = Storage::putFileAs(
            $patz, $request->file('ddt_file'), $fileName
        );
       
        $ddt_file_url= new Ddt_file_url();  //salvare ddt e poi prendere id
        $ddt_file_url->fileUrl = $pathComplete;
        $ddt_file_url->fileName = $fileName;
        $ddt_file_url->company()->associate(Auth::user()->id);
        $ddt_file_url->save();
        return $ddt_file_url;
    }

And for the showing part I'm using:

@if($ddtS->ddt_file_url_id)
     <a href="{{$ddt_urls->where('id',$ddtS->ddt_file_url_id)->first()->fileUrl}}"
               download="{{$ddt_urls->where('id',$ddtS->ddt_file_url_id)->first()->fileName}}">
           <button  type="button" class="btn btn-default">
                        Download ddt file <span class="fa icon-icona_download-01"></span>
            </button>
  </a>
@else
 <p>-</p>
@endif 

Keep in mind that the user's folder are under Storage/users/username_id and all storage is linked to public with the artisan istruction "php artisan storage:link"

Please sign in or create an account to participate in this conversation.