在非对象层级4.2

时间:2016-09-25 19:56:47

标签: php laravel laravel-4 image-uploading intervention

我试图建立一个上传图片类,我传递文件和路线,然后上传图片这是代码

这是表格

<form class="new-user-form row" method="POST" action="{{ URL::to('administrador/producto/nuevo/enviar') }}" enctype="multipart/form-data"> 
    <input type="file" name="img[]">
</form>

它发送给控制器

$file = Input::file();
    foreach($file['img'] as $f)
    {
        $img = new ItemImage;
        $img->item_id = $id;
        /*if i return $f->getClientOriginalName() here it return the name of
        the file without error.
        if i return var_dump($f) here it will show the response below.
        */
        $img->image   = ImageManager::upload_image($f,'images/items'); 
        $img->save();
    }

班级看起来像这样

<?php
Class ImageManager
{
    function __construct() {
    }

    public static function upload_image(&$file, $path)
    {
        $ruta    = $path;
        //if i return $file in here it return the tmp path
        //if i return var_dump($file) here it return the response below
        $extension = File::extension($file->getClientOriginalName());//<-this line trow the error

        $time = time();
        $miImg = $time.'.'.$extension;
        while (file_exists($ruta.$miImg)) {
            $time = time();
            $miImg = $time.'.'.$extension;
        }
        $file->move($ruta,$miImg);
        return $miImg;
    }
}

当我调用foreach中的上传代码时,它会起作用,但是当我从类中调用它时会触发错误。

如果我在控制器上使用代码而不是函数,但是我需要全局调用函数,我使用intervention/image来操作图像。

为什么会这样?我该如何解决?

EDITE

在这两种情况下,在控制器和我制作的自定义类中,当我var_dump file它显示此响应时

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(6) "l2.jpg"
  ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  string(10) "image/jpeg"
  ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(10737)
  ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(0)
  ["pathName":"SplFileInfo":private]=>
  string(24) "C:\xampp\tmp\php782D.tmp"
  ["fileName":"SplFileInfo":private]=>
  string(11) "php782D.tmp"
}

如果它是UploadedFile实例,为什么要抛出错误,为什么不能在我的自定义类上使用该函数?

0 个答案:

没有答案