本文介紹了Laravel 5.3 Storage::put 創(chuàng)建一個包含文件名的目錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用 Laravel 的文件存儲功能來保存文件:
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);
}
}
}
這確實保存了文件,但保存在與文件同名的目錄中,例如:
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
這是預期的嗎?有沒有辦法只存儲文件而不為每個文件設置單獨的目錄?
Is this expected? Is there any way to just store the file without a separate directory for each file?
謝謝!
推薦答案
你需要在第二個參數(shù)中提供文件內(nèi)容而不是文件對象,試試這個:
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));
這篇關于Laravel 5.3 Storage::put 創(chuàng)建一個包含文件名的目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!