2014-04-24 6 views

Odpowiedz

37

Patrz: file_exists()

Zastosowanie:

if (!file_exists($path)) { 
    // path does not exist 
} 

W laravel:

if(!File::exists($path)) { 
    // path does not exist 
} 
+0

pracował jak urok. Dzięki, panie Martin, Uratowałeś mi dzień. –

3

Zalecanym sposobem jest użycie

if (!File::exists($path)) 
{ 

} 

Zobacz source code

Jeśli spojrzeć na kod, to dzwoni file_exists()

0

normalnie tworzyć losowe foldery wewnątrz obrazów dla każdego pliku to pomaga trochę w szyfrowanie adresów URL, a zatem publiczne znajdą to hardr aby zobaczyć pliki po prostu wpisując adres URL do Twojego katalogu.

// Check if Logo is uploaded and file in random folder name - 
if (Input::hasFile('whatever_logo')) 
      { 
       $destinationPath = 'uploads/images/' .str_random(8).'/'; 
       $file = Input::file('whatever_logo'); 
       $filename = $file->getClientOriginalName();     
       $file->move($destinationPath, $filename); 
       $savedPath = $destinationPath . $filename; 
       $this->whatever->logo = $savedPath; 
       $this->whatever->save(); 
      } 

      // otherwise NULL the logo field in DB table. 
      else 
      { 
       $this->whatever->logo = NULL;  
       $this->whatever->save();  
      }    
7

Z laravel można użyć:

$path = public_path().'/images'; 
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); 

Nawiasem mówiąc, można także umieścić podfoldery jako argument w funkcji pomocnika ścieżka laravel, podobnie jak to:

$path = public_path('images/'); 
+1

Podoba mi się eleganckie użycie 'lub 'w tej odpowiedzi. – tishma

Powiązane problemy