Laravel 无法删除文件
Laravel file cannot be deleted
我正在尝试删除位于 storage/framework/cache/
.
下的文件(我在导入命令失败时创建的临时文件)
我尝试使用 unlink
但总是出现错误 Text file busy
(正如我搜索的那样,显示此错误是因为文件正在使用中)。
我也试过 File::delete($fullpath)
但也不行(没有错误,但文件没有被删除)
问题是如果我检查文件是否存在,它总是正确的(我也手动检查并且文件在那里)。
这是我做的:
$lockfilePath = $this->lockFilePath($uri);// if i display this line, I get /var/www/html/project/storage/framework/cache/filename.lock
if (!file_exists($lockfilePath)) {
throw new RuntimeException("$uri was not locked. Lockfile not found.");
}
\File::delete($lockfilePath); // I tried also unlink($lockfilePath);
我正在使用 homestead 和 Laravel 5.1(PHP 5.6 版)。
如何生成文件?
创建后是否关闭文件?
fclose($file)
您也可以尝试在关闭文件之后和取消链接之前强制进行垃圾回收:
gc_collect_cycles();
确保关闭文件并且不使用它。
也可以尝试 Storage::delete($filepath)
并确保您有权删除该文件,因为它位于 laravel storage
中
试试下面的代码:
在 class 开始之前使用 "File" class 例如 use File;
$lockfilePath = $this->lockFilePath($uri);
if (File::exists($lockfilePath)) {
unlink($lockfilePath);
} else {
return "file not present";
}
注意:确保您的项目对要删除的文件具有读写权限运行下面的代码提供读写权限。
chmod -R 777 /var/www
( -R 使其递归)
最好的解决方法是使用unlink($filepath)
,但在此之前,您需要搜索打开文件的位置并使用fclose($handle)
经过一番搜索,我找到了这个
假设你的文件在这个路径下storage/app/public/images
控制器
.... other logic code
if( Storage::disk('local')->exists('public/images/'.$file->file_system_path))
Storage::disk('local')->delete('public/images/'.$file->file_system_path);
两个方法return一个布尔值。
重要提示!
php artisan storage:link
/config/filesystem.php
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
我正在尝试删除位于 storage/framework/cache/
.
下的文件(我在导入命令失败时创建的临时文件)
我尝试使用 unlink
但总是出现错误 Text file busy
(正如我搜索的那样,显示此错误是因为文件正在使用中)。
我也试过 File::delete($fullpath)
但也不行(没有错误,但文件没有被删除)
问题是如果我检查文件是否存在,它总是正确的(我也手动检查并且文件在那里)。
这是我做的:
$lockfilePath = $this->lockFilePath($uri);// if i display this line, I get /var/www/html/project/storage/framework/cache/filename.lock
if (!file_exists($lockfilePath)) {
throw new RuntimeException("$uri was not locked. Lockfile not found.");
}
\File::delete($lockfilePath); // I tried also unlink($lockfilePath);
我正在使用 homestead 和 Laravel 5.1(PHP 5.6 版)。
如何生成文件?
创建后是否关闭文件?
fclose($file)
您也可以尝试在关闭文件之后和取消链接之前强制进行垃圾回收:
gc_collect_cycles();
确保关闭文件并且不使用它。
也可以尝试 Storage::delete($filepath)
并确保您有权删除该文件,因为它位于 laravel storage
试试下面的代码:
在 class 开始之前使用 "File" class 例如 use File;
$lockfilePath = $this->lockFilePath($uri);
if (File::exists($lockfilePath)) {
unlink($lockfilePath);
} else {
return "file not present";
}
注意:确保您的项目对要删除的文件具有读写权限运行下面的代码提供读写权限。
chmod -R 777 /var/www
( -R 使其递归)
最好的解决方法是使用unlink($filepath)
,但在此之前,您需要搜索打开文件的位置并使用fclose($handle)
经过一番搜索,我找到了这个
假设你的文件在这个路径下storage/app/public/images
控制器
.... other logic code
if( Storage::disk('local')->exists('public/images/'.$file->file_system_path))
Storage::disk('local')->delete('public/images/'.$file->file_system_path);
两个方法return一个布尔值。
重要提示!
php artisan storage:link
/config/filesystem.php
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],