Laravel 调用非对象上的成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28201568/
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 10:47:00  来源:igfitidea点击:

Laravel Call to a member function on a non-object

phpooplaravel

提问by Rafael

I have searched on stack and found many questions like this...

我在堆栈上搜索过,发现了很多这样的问题......

However, I am sure that I am using an object and have verified that in the dump.

但是,我确信我正在使用一个对象并在转储中验证了它。

Error

错误

>16: Call to a member function getRealPath() on a non-object

>16: Call to a member function getRealPath() on a non-object

Line 16

16号线

$image->getRealPath();

$image->getRealPath();

Var Dump $image

变量转储 $image

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(41) "5a862f92da957da3e0208357ce006afd_970x.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(346047) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(24) "C:\xampp\tmp\phpA5C7.tmp" ["fileName":"SplFileInfo":private]=> string(11) "phpA5C7.tmp" } 

If I dump $image->getRealPath();I get a string(24) "C:\xampp\tmp\phpE193.tmp"which is what I want. I have no idea why it's saying $image is not an object when clearly it is.

如果我转储,$image->getRealPath();我会得到一个string(24) "C:\xampp\tmp\phpE193.tmp"我想要的。我不知道为什么它说 $image 显然不是一个对象。

Controller

控制器

# Process Images - Queue?
if ($filesUploaded) {
    $images = Input::file('images');
    foreach ($images as $image) {
        # Record Creation
        $record = new Image;
        $record->user_id = Auth::id();
        $record->ad_id = $adId;
        $record->name = Str::random();
        $record->save();
        # Create TN
        $record->createTN($image);
        # Create Smalls
        # Create Larges
    }
}

Image class

图片类

public function createTN($image) {
    $PUBLIC_PATH = public_path();

# Load Zebra Image Library
require_once $PUBLIC_PATH.'/uploads/Zebra_Image.php';

$destinationPath = $PUBLIC_PATH.'/uploads/thumbnails/';
$tn = new Zebra_Image();
$tn->source_path = $image->getRealPath();
$tn->target_path = $destinationPath.$this->name.'_sm.jpg';
$tn->jpeg_quality = 60;
$tn->preserve_aspect_ratio = true;
$tn->enlarge_smaller_images = true;
$tn->resize(100, 100, ZEBRA_IMAGE_CROP_CENTER);
}

回答by Alan Storm

Looking at the original loop

查看原始循环

$images = Input::file('images');
foreach ($images as $image) {
    # Record Creation
    $record = new Image;
    $record->user_id = Auth::id();
    $record->ad_id = $adId;
    $record->name = Str::random();
    $record->save();
    # Create TN
    $record->createTN($image);
    # Create Smalls
    # Create Larges
}

It looks like you're processing a request with more than a single file upload. The form, javascript code, or flash uploader code you're using to post to this Laravel route looks like it's sending at least one file (or perhaps a blank file entry) that Laravel's request object can't process into an object. A loop like this might reveal more about what's going on

看起来您正在处理具有多个文件上传的请求。您用来发布到此 Laravel 路由的表单、javascript 代码或 Flash 上传器代码看起来至少发送了一个 Laravel 的请求对象无法处理的文件(或者可能是一个空白文件条目)。像这样的循环可能会揭示更多关于正在发生的事情

foreach($images as $image)
{
    var_dump($image);
}

Most of those will be Symfony\Component\HttpFoundation\File\UploadedFileobjects, but I bet at least one of them is a null. You could also try var_dump($_FILES)and look for any obvious errors or empty entries -- maybe something that's too large for the server to process?

其中大部分将是Symfony\Component\HttpFoundation\File\UploadedFile对象,但我敢打赌其中至少一个是null. 您还可以尝试var_dump($_FILES)查找任何明显的错误或空条目——也许是服务器无法处理的内容?

The reason you couldn't use lukasgeiter'ssolution is the Symfony\Component\HttpFoundation\File\UploadedFileclass has PHP's internal SplFileInfoclass as an ancsetor, and until very recentlyPHP had a bug/feature where a programmer couldn't cast SplFileInfoas a boolean (using the using !converts the variable to a boolean type for the comparision)

您不能使用lukasgeiter 的解决方案的原因是Symfony\Component\HttpFoundation\File\UploadedFile该类具有 PHP 的内部SplFileInfo类作为祖先,直到最近PHP 有一个错误/功能,程序员无法将其转换SplFileInfo为布尔值(使用 using!将变量转换为布尔值输入比较)

Finally, Laravel relies on Symfony components for its file upload functionality. I believe you can find the Symfony\Component\HttpFoundation\File\UploadedFileinstantiations here

最后,Laravel 依赖 Symfony 组件来实现其文件上传功能。我相信你可以在Symfony\Component\HttpFoundation\File\UploadedFile这里找到实例

#File: vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/FileBag.php
protected function convertFileInformation($file)
{
    if ($file instanceof UploadedFile) {
        return $file;
    }

    $file = $this->fixPhpFilesArray($file);
    if (is_array($file)) {
        $keys = array_keys($file);
        sort($keys);

        if ($keys == self::$fileKeys) {
            if (UPLOAD_ERR_NO_FILE == $file['error']) {
                $file = null;
            } else {
                $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
            }
        } else {
            $file = array_map(array($this, 'convertFileInformation'), $file);
        }
    }

    return $file;
}

So if you're a debug at the source sort of person, start here.

因此,如果您是源头调试人员,请从这里开始。

回答by lukasgeiter

One of your images probably is not set. So when you check (the first) with a var_dumpeverything seems alright. But when the full loop runs it throws an exception on the second, third, ... iteration.

您的其中一张图片可能未设置。因此,当您使用 a 检查(第一个)时,var_dump一切似乎都很好。但是当完整循环运行时,它会在第二次、第三次、...迭代中抛出异常。

This should fix it:

这应该解决它:

foreach ($images as $image) {
    if(!is_object($image)) continue;
    # Record Creation
    $record = new Image;
    $record->user_id = Auth::id();
    $record->ad_id = $adId;
    $record->name = Str::random();
    $record->save();
    # Create TN
    $record->createTN($image);
    # Create Smalls
    # Create Larges
}