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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24884009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:50:29  来源:igfitidea点击:

Laravel FileNotFoundException thrown even though file exists

phplaravellaravel-4

提问by kajetons

I ran into a problem today to which I can't seem to find a solution.

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

I'm creating a class for image upload. The upload itself works fine, but when I try to use the uploaded file for creating a thumbnail, for some reason the file can't be found.

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

My code:

我的代码:

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;
}

I know the file gets uploaded, but I have no idea why it can't be found. I've tried the same action using php artisan tinkerand it works there, so the problem is not in the file path.

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

The only idea that I've come up so far is that is related to directory permissions, but I haven't been able to verify it yet.

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

采纳答案by Eternal1

I believe your problem is here: $realPath = 'public/' . $filePath;. You need full path to your upload folder, so try replacing it with public_path()."/".$filePath.

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

回答by Sotiris Raptis

I have fallen in the same situation recently, so here is a hint

我最近也陷入了同样的情况,所以这里有一个提示

Laravel after version 5 seems to allow dealing with files (get/put) only when using declared storage defined into filesystems.php

版本 5 之后的 Laravel 似乎只允许在使用定义到 filesystems.php 中的声明存储时处理文件(获取/放置)

In that case the user must use 'local' or 'public' storage for getting the image or file stored. According to the code above, please do the following

在这种情况下,用户必须使用“本地”或“公共”存储来存储图像或文件。根据上面的代码,请执行以下操作

  1. edit config/filesystems.phpfind local and make the following change
  1. 编辑config/filesystems.php找到本地并进行以下更改
'local' => [
   'driver' => 'local',
   'root' => storage_path('app/local/common'),
],
  1. In order to read the file (ie 'image1.jpg') (get)
  1. 为了读取文件(即'image1.jpg')(获取)
$afile = storage::disk('public')->get('image1.jpg');

In your case:

在你的情况下:

$realpath =  'image1.jpg'; //**Just the filename**
$image = Image::make(storage::disk('local')->get($realPath));

Hope this helps

希望这可以帮助