簡體   English   中英

PHP / Laravel文件存在驗證不起作用

[英]PHP/Laravel file existence verification not working

我有一個<form>其中包含多個文件輸入,這些文件將這些文件存儲到服務器中。 文件名必須遵循數字標准,這就是為什么我在存儲之前重命名它們的原因。 每個帖子都有一個ID,並且可以包含多個文件,因此模式為:post-id +破折號和數字+文件擴展名,這樣,如果同一帖子具有兩個具有相同擴展名的文件,則破折號+數字將避免它們將被覆蓋。 因此,在存儲文件之前,我運行了一個循環以查找正確的名稱編號。 問題是,用於驗證文件是否存在的函數似乎在應有的情況下未返回true。 編碼:

$counter = 0;
do{
  $nomeArquivo = $post->id . "-{$counter}"  . "." . $arq->extension();

  $counter++;
//these commented are other ways of verification I tried
//}while(Storage::exists($nomeArquivo));
//}while(file_exists("/storage/" . $nomeArquivo));
}while(is_file("/storage/" . $post->id . "-{$counter}"));

Storage::putFileAs('/public', $arq, $nomeArquivo);

上面的代碼在foreach($files as $arq)內部運行,其中$files是來自表單輸入的文件。

參考: https : //laravel.com/docs/5.4/filesystemhttps://laravel.com/api/5.4/Illuminate/Filesystem/Filesystem.html

使用文件外觀檢查文件是否存在:

File :: exists($ myfile)

http://laravel-recipes.com/recipes/123/determining-if-a-file-exists

如果文件名僅需要唯一,

您可以這樣指定唯一名稱:

$file = $request->file('file');
$extension = $file->getClientOriginalExtension(); 
$destination ='/files/';
$filename = uniqid() . '.' . $extension;
$file->move($destination, $filename);

現在,將上述文件名保存在數據庫中。 希望對您有幫助

正如用戶@ fubar在評論中指出的那樣,我引用了錯誤的路徑來驗證文件的存在。 我將while條件更改為while(\\File::exists(public_path() . '/storage/' . $nomeArquivo)) ,並且效果很好。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM