我正在使用File::copy將上傳的檔案從主檔案夾復制到我的目標檔案夾。但它一直說沒有這樣的檔案或目錄。
public function store(Request $request)
{
$data = $request-> validate([
'name' => 'required',
'product_id' => 'required',
'price' => 'required',
'file' => 'required',
'file.*' => 'mimes:jpeg,jpg,png,svg,gif,csv,xlsx,pdf,docx'。
'publish' => 'required',
]);
$fileData =【】。
if($request-> hasFile('file')
{
foreach($request->file('file') as $file)
{
$path = public_path('storage/documents/'.$request-> product_id)。
$fileName = time().'_.$file->getClientOriginalName()。
$file->move($path, $fileName) 。
$target = public_path('storage/documents/download')。
File::copy($path.$fileName, $target.$fileName); // errors here.
$fileData[] = $fileName;
}
$data['file'] = json_encode($fileData)。
}
$document = Document::create($data)。
return redirect()->route('product.index')->with('success','Document added successfully') 。
}
我怎樣才能解決這個問題,或者有什么其他方法可以在兩個檔案夾中上傳同一個檔案?
uj5u.com熱心網友回復:
$file->move($path, $fileName);
你正在移動檔案,首先檢查它是否被移動到那里。
$target = public_path('storage/documents/download')。 File::copy($path.$fileName, $target.$fileName); //錯誤在這里
你的錯誤似乎與你移動的檔案在該目錄中是否正確移動有關。如果命名正確,應該可以解決這個問題
File::copy(base_path('test.text'),base_path('public/test.text')) 。
檢查名稱并移動檔案。
uj5u.com熱心網友回復:
你可能需要在你的路徑和檔案名之間有一個目錄分隔符,像這樣:
File::copy($path. $fileName, $target.$fileName); // errors here.
File::copy($path.'/'.$fileName, $target. '/'.$fileName); //錯誤已糾正。
uj5u.com熱心網友回復:
你必須對你的代碼進行除錯。遵循這些步驟,或者
- 試著列印出一個新的代碼。
- 嘗試列印檔案路徑的值。如果你發現有什么不對,請改變它。
- 打開組態檔夾中的
session.php檔案,尋找storage_path。
- 改變你的檔案夾權限,但它說沒有目錄,所以可能是其他問題。
- 通過使用
php artisan cache:clear或php artisan config:clear清除你的配置或其他快取。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310458.html
標籤:
上一篇:安卓11存盤訪問和Java檔案庫
下一篇:自動生成基于成員的操作?
