du13932014807 2017-08-09 12:05
浏览 48
已采纳

尝试解析文件,接收与SplFileInfo有关的错误

I am saving an image with the current User object and even though i'm not yielding any wanted results. I am trying to parse an array of files, in that array, I am trying to get the object key that is named filename.

I currently have 2 images in this file array:

array:2 [▼
  0 => SplFileInfo {#219 ▶}
  1 => SplFileInfo {#220 ▶}
]

Each array key has an object and in that object I am looking for the filename key:

filename: "oIWVAvlOyG9yMzhhvTFoYX2wGNTa2pEth7gb228Z.png"

If it really matters, my code is on a controller on Laravel 5.4:

public function index(Request $request)
    {
        $user = Auth::user();
        $userImagesPath = public_path().'\\storage\\'.$user->email;
        $userImagesArray = File::allFiles($userImagesPath);
        $contents =[];
        $fileNameArray = [];
        foreach ($userImagesArray as $file){
            $contents[] = $file;
            foreach ($contents as $fileName){
                $fileNameArray[] = $fileName->filename;
            }
        }
        dd($fileNameArray);
        return view('sharmutaView')
            ->with('user', $user);
    }

I am getting an error: Undefined property: Symfony\Component\Finder\SplFileInfo::$filename, which is clearly telling me I ain't parsing right.

  • 写回答

1条回答 默认 最新

  • dsjfrkvn818747 2017-08-09 12:13
    关注

    The userImagesArray is the array you provided?

    Try this:

    public function index(Request $request)
        {
            $user = Auth::user();
            $userImagesPath = public_path().'\\storage\\'.$user->email;
            $userImagesArray = File::allFiles($userImagesPath);
            $fileNameArray = [];
            foreach ($userImagesArray as $file){
                    $fileNameArray[] = $file->getFilename();
                }
            }
            dd($fileNameArray);
            return view('sharmutaView')
                ->with('user', $user);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看