文件存在在 laravel 中无法正常工作
File exist not working in laravel for a file that right there
在我的 laravel 项目中,我有一个用于上传和删除文件的表单。
我在 config/filesystem:
中创建了一个磁盘
'upload' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL'),
'visibility' => 'public',
],
此处上传文件正常,但当我想将它们从 public 文件夹中删除时,文件不存在:
$img_path = public_path( $this->directory . '/' . $this->getFliename());
$img_path = str_replace( "/",'\', $img_path); // checked with it & without it
// $img_path= "C:\Users\Me\Desktop\final\public\upload\usersbc722d2b7c05.jpg"
dump(File::exists($img_path)); // it always return false
// and then which one
File::delete($img_path);
Storage::disk('upload')->delete($img_path);
为什么这个文件不存在,删除文件哪一个是正确的?
用route做就更复杂了???
// testing ...
Route::get('/test', function (){
File::delete('C:\Users\Me\Desktop\final\public\upload\usersbc722d2b7c05.jpg');
});
当您将图像存储在 public/upload 文件夹中时:
$user_id= $user->id;
$file_name=$user->image;
$img_path=public_path('upload/users/'.$user_id. '/' .$filename);
if(is_file($img_path)){
unlink($img_path);
// or File::delete($img_path);
}else{
echo "File does not exist";
}
该文件可能存在于文件系统中,但 PHP 所在的网络用户 运行 可能没有读取、更新或删除该文件的权限。
在 linux 或 mac 系统上,您通常需要授予网络用户文件的所有权,并将文件权限设置为 664,将文件夹权限设置为 775。
644 和 755 应该也可以,并为您提供更严格的权限。
如果上述方法失败,请尝试使用 777 进行调试,但不要在生产环境中这样做,因为这是一个严重的安全问题。
我不确定 Windows 是如何完成的,但希望这能让你走上正确的道路。
您的 $img_path 似乎设置了文件的完整路径,而不是 $file->storeAs()
在您保存时返回的相对于指定磁盘基址的路径文件。
这应该有效:
Storage::disk('upload')->delete('upload/users/1/5bc722d2b7c05.jpg')
如果你出于某种原因需要磁盘上的完整路径,试试这个:
Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg')
检查文件是否存在:
Storage::disk('upload)->exists('upload/users/1/5bc722d2b7c05.jpg')
或 File::exists(Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg'))
(returns C:\Users\Me\Desktop\final\public\upload/users/1/5bc722d2b7c05.jpg
因为你的 'upload' 磁盘的基数是 public_path()
)
Web 服务器的 Read/write 权限也可能发挥作用,具体取决于您的服务器配置。通常,您不会将内容直接存储在 public/
文件夹中,而是存储在 storage/app/public
中并使用 php artisan storage:link
为存储目录生成符号 link。您可以在这里阅读更多内容:https://laravel.com/docs/5.7/filesystem#the-public-disk
作为旁注,Windows 接受 /
或 \
,因此使用 /
使您的脚本更容易在其他系统上使用,并减少在字符串转义。
在我的 laravel 项目中,我有一个用于上传和删除文件的表单。 我在 config/filesystem:
中创建了一个磁盘'upload' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL'),
'visibility' => 'public',
],
此处上传文件正常,但当我想将它们从 public 文件夹中删除时,文件不存在:
$img_path = public_path( $this->directory . '/' . $this->getFliename());
$img_path = str_replace( "/",'\', $img_path); // checked with it & without it
// $img_path= "C:\Users\Me\Desktop\final\public\upload\usersbc722d2b7c05.jpg"
dump(File::exists($img_path)); // it always return false
// and then which one
File::delete($img_path);
Storage::disk('upload')->delete($img_path);
为什么这个文件不存在,删除文件哪一个是正确的?
用route做就更复杂了???
// testing ...
Route::get('/test', function (){
File::delete('C:\Users\Me\Desktop\final\public\upload\usersbc722d2b7c05.jpg');
});
当您将图像存储在 public/upload 文件夹中时:
$user_id= $user->id;
$file_name=$user->image;
$img_path=public_path('upload/users/'.$user_id. '/' .$filename);
if(is_file($img_path)){
unlink($img_path);
// or File::delete($img_path);
}else{
echo "File does not exist";
}
该文件可能存在于文件系统中,但 PHP 所在的网络用户 运行 可能没有读取、更新或删除该文件的权限。
在 linux 或 mac 系统上,您通常需要授予网络用户文件的所有权,并将文件权限设置为 664,将文件夹权限设置为 775。
644 和 755 应该也可以,并为您提供更严格的权限。
如果上述方法失败,请尝试使用 777 进行调试,但不要在生产环境中这样做,因为这是一个严重的安全问题。
我不确定 Windows 是如何完成的,但希望这能让你走上正确的道路。
您的 $img_path 似乎设置了文件的完整路径,而不是 $file->storeAs()
在您保存时返回的相对于指定磁盘基址的路径文件。
这应该有效:
Storage::disk('upload')->delete('upload/users/1/5bc722d2b7c05.jpg')
如果你出于某种原因需要磁盘上的完整路径,试试这个:
Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg')
检查文件是否存在:
Storage::disk('upload)->exists('upload/users/1/5bc722d2b7c05.jpg')
或 File::exists(Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg'))
(returns C:\Users\Me\Desktop\final\public\upload/users/1/5bc722d2b7c05.jpg
因为你的 'upload' 磁盘的基数是 public_path()
)
Read/write 权限也可能发挥作用,具体取决于您的服务器配置。通常,您不会将内容直接存储在 public/
文件夹中,而是存储在 storage/app/public
中并使用 php artisan storage:link
为存储目录生成符号 link。您可以在这里阅读更多内容:https://laravel.com/docs/5.7/filesystem#the-public-disk
作为旁注,Windows 接受 /
或 \
,因此使用 /
使您的脚本更容易在其他系统上使用,并减少在字符串转义。