File::exists return 已排队的电子邮件作业中的现有文件为 false

File::exists return false for existing file in email job queued

我正在尝试使用 Laravel 的队列发送电子邮件,在这封电子邮件中,如果文件存在,我会使用此功能显示用户的个人资料图片:

public function getProfilePictureAttribute()
{
    $path = 'users/' . $this->id . '/picture.png';
    if (File::exists(public_path() . $path)) {
        return $path;
    } else {
        return $this->picture_default;
    }
}

我在网络上使用了相同的功能并且它工作正常,但在电子邮件中,所以通过 artisan 和 table 工作,我总是得到默认图片。 我想不通。

我觉得你的文件路径有问题,但是需要像这样使用绝对路径

$absulate_path = 'your absulate path';
// E:/xampp/htdocs/test_projuect      
$path = 'users/' . $this->id . '/picture.png';
 if (file_exists($absulate_path.'/'.$path)) {
   /* do your code
 } else {
       echo "file not exit";
 }

我想这对你有帮助

问题解决者:

public function getProfilePictureAttribute()
{
    $path = 'users/' . $this->id . '/picture.png';
    if (File::exists(realpath(public_path() . $path))) {
        return $path;
    } else {
        return $this->picture_default;
    }
}