文件移动到另一个文件夹 laravel
File move to another folder laravel
我需要最近 7 天的存储日志才能移动新文件夹。但是,我无法移动它们并出现此错误。
rename(/var/www/html/eMarketing/storage/logs/old-log-2020-02-27,/var/www/html/eMarketing/storage/logs/laravel-2020-02-27.log): Not a directory
我的代码在这里
public function logs()
{
$today = \Carbon\Carbon::today()->format('Y-m-d');
$days = \Carbon\Carbon::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);
}
move()
方法可用于重命名现有文件或将现有文件移动到新位置。但
$newDirectoryPath
是文件夹而不是文件。
解决方案
您需要更改:
\File::move(
$path . $file['basename'], // old file
$newDirectoryPath . '/' . $file['basename'] // new file
);
public function logs()
{
$today = \Carbon\Carbon::today()->format('Y-m-d');
$days = \Carbon\Carbon::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']);
}
}
}
我需要最近 7 天的存储日志才能移动新文件夹。但是,我无法移动它们并出现此错误。
rename(/var/www/html/eMarketing/storage/logs/old-log-2020-02-27,/var/www/html/eMarketing/storage/logs/laravel-2020-02-27.log): Not a directory
我的代码在这里
public function logs()
{
$today = \Carbon\Carbon::today()->format('Y-m-d');
$days = \Carbon\Carbon::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);
}
move()
方法可用于重命名现有文件或将现有文件移动到新位置。但
$newDirectoryPath
是文件夹而不是文件。
解决方案
您需要更改:
\File::move(
$path . $file['basename'], // old file
$newDirectoryPath . '/' . $file['basename'] // new file
);
public function logs()
{
$today = \Carbon\Carbon::today()->format('Y-m-d');
$days = \Carbon\Carbon::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']);
}
}
}