如何确定文件是否存在于Laravel 5.2的公共目录中

如何确定文件是否存在于Laravel 5.2的公共目录中,laravel,laravel-5.2,Laravel,Laravel 5.2,我在public/image目录中有一些图像文件,所以我想在保存新文件之前确定该目录中是否存在文件。 如何确定文件是否存在?您可以使用存储外观: Storage::disk('image')->exists('file.jpg'); // bool 如果您正在使用如上所示的磁盘图像,则需要在config/filesystems.php中定义一个新磁盘,并在磁盘数组中添加以下条目: 'image' => [ 'driver' => 'local', 'root'

我在public/image目录中有一些图像文件,所以我想在保存新文件之前确定该目录中是否存在文件。
如何确定文件是否存在?

您可以使用存储外观:

Storage::disk('image')->exists('file.jpg'); // bool
如果您正在使用如上所示的磁盘
图像
,则需要在
config/filesystems.php
中定义一个新磁盘,并在
磁盘
数组中添加以下条目:

'image' => [
    'driver' => 'local',
    'root' => storage_path('app/public/image'),
    'visibility' => 'public',
],
如果您想了解有关该外观的更多信息,请参阅以下文档:


希望有帮助:)

您可以使用存储外观:

Storage::disk('image')->exists('file.jpg'); // bool
如果您正在使用如上所示的磁盘
图像
,则需要在
config/filesystems.php
中定义一个新磁盘,并在
磁盘
数组中添加以下条目:

'image' => [
    'driver' => 'local',
    'root' => storage_path('app/public/image'),
    'visibility' => 'public',
],
如果您想了解有关该外观的更多信息,请参阅以下文档:


希望有帮助:)

您可以按照建议使用Laravel的存储外观。但是,您也可以使用PHP的内置函数轻松完成这一任务:

if (is_file('/path/to/foo.txt')) {
    /* The path '/path/to/foo.txt' exists and is a file */
} else {
    /* The path '/path/to/foo.txt' does not exist or is not a file */
}

您可以按照建议使用Laravel的存储外观。但是,您也可以使用PHP的内置函数轻松完成这一任务:

if (is_file('/path/to/foo.txt')) {
    /* The path '/path/to/foo.txt' exists and is a file */
} else {
    /* The path '/path/to/foo.txt' does not exist or is not a file */
}

您可以使用这个小实用程序检查目录是否为空

if($this->is_dir_empty(public_path() ."/image")){ 
   \Log::info("Is empty");
}else{
   \Log::info("It is not empty");
}

public function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
     if ($entry != "." && $entry != "..") {
     return FALSE;
     }
  }
  return TRUE;
}

您可以使用这个小实用程序检查目录是否为空

if($this->is_dir_empty(public_path() ."/image")){ 
   \Log::info("Is empty");
}else{
   \Log::info("It is not empty");
}

public function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
     if ($entry != "." && $entry != "..") {
     return FALSE;
     }
  }
  return TRUE;
}
文件存在(公共路径($name)

下面是我的解决方案,在下载文件之前检查文件是否存在

如果(文件存在(公共路径($name)))
return response()->下载(public_path($name));
文件存在(公共路径($name)

下面是我的解决方案,在下载文件之前检查文件是否存在

如果(文件存在(公共路径($name)))
return response()->下载(public_path($name));