問題描述
我正在使用 Laravel 的文件存儲(chǔ)功能來保存文件:
I'm using Laravel's file storage functionality to save a file:
public function dataPost(Request $request) {
$fileInForm = 'doc';
if ($request->hasFile($fileInForm)) {
$file = $request->file($fileInForm);
if ($file->isValid()) {
// Filename is hashed filename + part of timestamp
$hashedName = hash_file('md5', $file->path());
$timestamp = microtime() * 1000000;
$newFilename = $hashedName . $timestamp . '.' . $file->getClientOriginalExtension();
Storage::disk('local')->put($newFilename, $file);
}
}
}
這確實(shí)保存了文件,但保存在與文件同名的目錄中,例如:
This does save the file, but inside a directory named the same as the file, for example:
storage/app/952d6c009.jpg/952d6c009.jpg
或
storage/app/234234234.jpg/234234234.jpg
這是預(yù)期的嗎?有沒有辦法只存儲(chǔ)文件而不為每個(gè)文件設(shè)置單獨(dú)的目錄?
Is this expected? Is there any way to just store the file without a separate directory for each file?
謝謝!
推薦答案
你需要在第二個(gè)參數(shù)中提供文件內(nèi)容而不是文件對(duì)象,試試這個(gè):
you need to provide the file contents in the second argument not file object, try this:
Storage::disk('local')->put($newFilename, file_get_contents($file));
這篇關(guān)于Laravel 5.3 Storage::put 創(chuàng)建一個(gè)包含文件名的目錄的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!