I am trying to save files in the DB using BLOB. I know it is not a very good practice but it needs to be done this way.
Anyway the problem that I am facing is that PDF files returned are not readable anymore. But all other files like docx, odt, txt, jpg and so on are working just fine when converted back from blob.
public function store(Request $request)
{
$data = $request->all();
$file = $request->file('file');
$data['data'] = base64_encode(file_get_contents($file));
$data['extension'] = $file->guessExtension();
$data['name'] = $file->getClientOriginalName();
$file = $this->fileRepo->create($data);
return jsend()->success()
->message("Resource Created Successfully")
->data($file->toArray())
->get();
}
public function download($id)
{
$file = $this->fileRepo->find($id);
$randomDir = md5(time() . $file->id . $file->user->id . str_random());
mkdir(public_path() . '/files/' . $randomDir);
$path = public_path() . '/files/' . $randomDir . '/' . html_entity_decode($file->name);
file_put_contents($path, base64_decode($file->data));
header('Content-Description: File Transfer');
return response()->download($path);
}
Where am I going wrong and is there a special way to store the PDFs in a blob field?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire