即使文件存在,也会抛出Laravel FileNotFoundException

kaj*_*ons 6 php laravel laravel-4

我今天遇到了一个问题,我似乎无法找到解决方案.

我正在为图片上传创建一个类.上传本身工作正常,但是当我尝试使用上传的文件创建缩略图时,由于某种原因无法找到该文件.

我的代码:

private static $imageTypes = [
    "image/jpeg", "image/png", "image/gif", "image/x-ms-bmp"
];

public function upload(UploadedFile $file, $folder = 'common')
{
    if (!$this->isImage($file)) {
        return false;
    }

    $path = $this->createPath($file, $folder);

    try {
        $file->move($path, $file->getClientOriginalName());
        $filePath = sprintf("%s/%s", $path, $file->getClientOriginalName());
    } catch (FileException $e) {
        return false;
    }

    $realPath = 'public/' . $filePath;
    //dd(File::exists($realPath)); - this returns false
    //dd(File::get($realPath)); - this throws the exception
    $image = Image::make(File::get($realPath));

    // Code for creating a thumbnail - not implemented yet.
    $thumbnailPath = '';

    return [
        'image' => $path,
        'thumbnail' => $thumbnailPath
    ];
}

private function isImage(UploadedFile $file)
{
    $type = $file->getClientMimeType();

    return in_array($type, self::$imageTypes);
}

private function createPath(UploadedFile $file, $folder)
{
    $time = Carbon::now();

    $path = sprintf(
        'Code/images/uploads/%s/%d-%d',
        $folder,
        $time->year,
        $time->month
    );

    return $path;
}
Run Code Online (Sandbox Code Playgroud)

我知道文件上传了,但我不知道为什么找不到它.我已尝试使用相同的操作php artisan tinker,它在那里工作,所以问题不在文件路径中.

我到目前为止唯一的想法是与目录权限相关,但我还没有能够验证它.

Ete*_*al1 7

我相信您的问题在这里:$realPath = 'public/' . $filePath;。您需要上传文件夹的完整路径,因此请尝试将其替换为public_path()."/".$filePath