2015-11-23 19 views
11

BLOBを使用してDBにファイルを保存しようとしています。私はそれが非常に良い習慣ではないが、このようにする必要があることを知っている。Laravel 5のBLOBから変換されたPDFを読み込めませんか?

とにかく私が直面している問題は、返されたPDFファイルがもう読めないということです。しかし、docx、odt、txt、jpgなどの他のすべてのファイルは、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); 
} 

どこが間違っていますか。どこにPDFをBLOBフィールドに保存する特別な方法がありますか?

+0

多分コメントはこちら(http://stackoverflow.com/a/8991231/717181)ですか? – Luceos

+0

私が知る限り、男は第三者のライブラリを使ってそれを長さ72の行でエンコードして解決したと言うことができますが、私のアプリケーションではそれをどうやって行うのか分かりません。特定の長さの行を使ってPHPでbase64でファイルをエンコードするには? – Rohan

+1

アップロード前とダウンロード後のファイルの違いは? 'diff before.pdf after.pdf'のようなものです。 –

答えて

1

ヘッダーが正しく返されていない可能性があり、ファイルが判読不能になっている可能性があります。

フォース置き換えることにより、それら:

header('Content-Description: File Transfer'); 
return response()->download($path); 

をして:

$headers = array(
    'Content-Description: File Transfer', 
    'Content-Type: application/octet-stream', 
    'Content-Disposition: attachment; filename="' . $file->name . '"', 
    ); 

return response()->download($path, $file->name, $headers); 
0

あなたはそのようなあなたのヘッダーのリターンを設定することができます。あなたはLaravelの応答ieを使用する必要があります。

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)); 

    return response()->download($path)->header('Content-Description', 'File Transfer'); 
} 
関連する問題