Home » Php » php – Uploaded file (base64) with Laravel is corrupt in public folder

php – Uploaded file (base64) with Laravel is corrupt in public folder

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have successfully decoded a base64 file and uploaded to a desired path with Laravel. However the file appears to be corrupt when I try accessing it. I’m not sure what the problem is.

Below is my script:

if($request->file){
        $file = $request->file;
        $base = base64_decode($file);

        $ready_file = time().'.' . explode('/', explode(':', substr($file, 0, strpos($file, ';')))[1])[1];
        $destinationPath = public_path() . "/uploads/user_files/" . $ready_file;             
        file_put_contents($destinationPath, $base);

    }
How to&Answers: