Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是什么原因导致无法在此Laravel 8应用程序中从服务器上物理删除映像?_Php_Laravel_Laravel 8 - Fatal编程技术网

Php 是什么原因导致无法在此Laravel 8应用程序中从服务器上物理删除映像?

Php 是什么原因导致无法在此Laravel 8应用程序中从服务器上物理删除映像?,php,laravel,laravel-8,Php,Laravel,Laravel 8,我正在开发一个需要用户注册和登录的Laravel8应用程序 在注册之后,用户可以用自己选择的图片替换默认的化身图像。他们还可以恢复到默认的化身(default.png) 删除后尝试从服务器中删除映像文件本身时出现问题 在routes\web.php中,我有: Route::post('/dashboard/profile/deleteavatar/{id}/{fileName}', [App\Http\Controllers\Dashboard\UserProfileController::cl

我正在开发一个需要用户注册和登录的Laravel8应用程序

在注册之后,用户可以用自己选择的图片替换默认的化身图像。他们还可以恢复到默认的化身(default.png)

删除后尝试从服务器中删除映像文件本身时出现问题

routes\web.php
中,我有:

Route::post('/dashboard/profile/deleteavatar/{id}/{fileName}', [App\Http\Controllers\Dashboard\UserProfileController::class, 'deleteavatar'])->name('profile.deleteavatar');
// Delete avatar
public function deleteavatar($id, $fileName) {
    $current_user = Auth::user();
    $current_user->avatar = "default.png";
    $current_user->save();

    if(File::exists(public_path('images/avatars' . $fileName))){
        File::delete(public_path('images/avatars' . $fileName));
    }
}
Http\Controllers\Dashboard\UserProfileController.php中,我有:

Route::post('/dashboard/profile/deleteavatar/{id}/{fileName}', [App\Http\Controllers\Dashboard\UserProfileController::class, 'deleteavatar'])->name('profile.deleteavatar');
// Delete avatar
public function deleteavatar($id, $fileName) {
    $current_user = Auth::user();
    $current_user->avatar = "default.png";
    $current_user->save();

    if(File::exists(public_path('images/avatars' . $fileName))){
        File::delete(public_path('images/avatars' . $fileName));
    }
}
app.js
中:

(function() {
    //Delete Avatar
    $('#delete-avatar').on('click', function(evt) {
        evt.preventDefault();
        var $avatar = $('#avatar-container').find('img');
        var $topAvatar = $('#top_avatar');
        var $trashIcon = $(this);
        var defaultAvatar = APP_URL + '/images/avatars/default.png';

        //Get user's ID
        var id = $(this).data('uid');
        var fileName = $avatar.attr('src').split('/').reverse()[0];

        if (confirm('Delete the avatar?')) {
            var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
            $.ajax({
                url: APP_URL + `/dashboard/profile/deleteavatar/${id}/${fileName}`,
                method: 'POST',
                data: {
                    id: id,
                    fileName: fileName,
                    _token: CSRF_TOKEN,
                },
                success: function() {
                    $avatar.attr('src', defaultAvatar);
                    $topAvatar.attr('src', defaultAvatar);
                    $trashIcon.remove();
                }
            });
        }
    });
})();
当我删除用户的化身时,会发生以下情况:

  • 用户表的
    化身
    列中,化身被替换为
    default.png
  • Chrome控制台显示
    500(内部服务器错误)
    错误
  • 图像不会从dearver中删除

  • 我缺少什么?

    如果您在公用文件夹中导入了图像,则说明您导入了错误的文件导入

    你应该进口

      use Illuminate\Support\Facades\File;
    
    然后我假设图像在
    public/images/avatars/avatar.png

    if(File::exists(public_path('images/avatars/' . $fileName))){
        File::delete(public_path('images/avatars/' . $fileName));
    }
    
    假设您有文件
    存储/app/public/images/avatars
    ,那么您应该执行以下操作

    if (Storage::exists('public/images/avatars/'. $fileName)) {
            Storage::delete('public/images/avatars/'. $fileName);
        }
    
    也不要忘记导入正确的一个

    use Illuminate\Support\Facades\Storage;
    

    这种方法很危险,但应在以下情况下使用:

    Http\Controllers\Dashboard\UserProfileController.php
    中,您可以使用@unlink来删除文件,如果文件存在,则忽略错误将被删除,如果不存在,则什么也不会发生

    //删除化身
    公共函数deleteavatar($id,$fileName){
    $current_user=Auth::user();
    $current_user->avatar=“default.png”;
    $current_user->save();
    @取消链接(公共路径('images/avatars/'.$fileName));
    }
    
    能否启用eror日志。请参见.env文件中的APP_DEBUG=true。文件是否在存储/APP/public中?是否检查了错误日志?哦,是的。我想我忘了导入和使用正确的外观。我一到电脑前就会查出来。