文件移动到另一个文件夹laravel



我需要最近7天的存储日志来移动一个新文件夹。但是,我无法移动它们,所以出现了这个错误。

rename(/var/www/html/eMarketing/storage/logs/old-log-200-02-27,/var/www/html/eMarketing/storage/logs/laravel-2020-02-27.log(:不是目录

我的代码在这里

public function logs() 
{
  $today = CarbonCarbon::today()->format('Y-m-d');
  $days  = CarbonCarbon::today()->subDays(7)->format('Y-m-d');
  $newDirectoryPath = storage_path('logs/old-log-'.$days);
  if (!File::isDirectory($newDirectoryPath)) {
      File::makeDirectory($newDirectoryPath);
  }
  $path = storage_path('logs/');
  $allFiles = File::allFiles($path);
  foreach($allFiles as $files) {
      $file = pathinfo($files);
      $logDay = str_replace('laravel-','', $file['filename']);  
      if ($logDay >= $days && $logDay < $today) {
          File::move($newDirectoryPath, $path.$file['basename']);
      }
   }
}

问题

问题是,您没有要移动的文件

$newDirectoryPath = storage_path('logs/old-log-' . $days);
if (!File::isDirectory($newDirectoryPath)) {
    File::makeDirectory($newDirectoryPath);
}

CCD_ 1方法可用于重命名现有文件或将现有文件移动到新位置。但是$newDirectoryPath是文件夹而不是文件。


解决方案

您需要更改:

File::move(
    $path . $file['basename'],                  // old file
    $newDirectoryPath . '/' . $file['basename'] // new file
);
public function logs()
{
    $today = CarbonCarbon::today()->format('Y-m-d');
    $days  = CarbonCarbon::today()->subDays(7)->format('Y-m-d');
    $newDirectoryPath = storage_path('logs/old-log-' . $days);
    if (!File::isDirectory($newDirectoryPath)) {
        File::makeDirectory($newDirectoryPath);
    }
    $path     = storage_path('logs/');
    $allFiles = File::allFiles($path);
    foreach ($allFiles as $files) {
        $file   = pathinfo($files);
        $logDay = str_replace('laravel-', '', $file['filename']);
        if ($logDay >= $days && $logDay < $today) {
            File::move($path . $file['basename'], $newDirectoryPath . '/' . $file['basename']);
        }
    }
}

最新更新