douxi2670 2019-02-21 07:19
浏览 91
已采纳

在null上调用成员函数guessExtension()

it gives the error that i wrote on the title. I've used this exact same code before but i doesnt work now. I think i doesnt get the "id" but i dont know why. if i uncomment those comment lines on the controller it opens the photo add window and lets me select a photo but after clicking submit, it doesnt upload. please guys i really need your help. any ideas? I've seen same erorr on stackoverflow here but that solution didnt work for me

*****This is my controller*****

/**
     * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"GET","POST"})
     */
    public function fotoekle(Request $request, $id, Duyurular $duyurular): Response
    {
        $form = $this->createForm(DuyurularType::class, $duyurular);
        $form->handleRequest($request);
        echo ('sefa');
       // if ($form->isSubmitted() && $form->isValid()) {
            echo ('sefa2');
            $file = $request->files->get('image');
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
  //          dump($request);
    //        die();
            // $file = $duyurular->getFotograf();
            // $file = $form->get('fotograf')->getData();

            try{
                $file->move(
                    $this->getParameter('fotograflar_directory'),
                    $fileName
                );
            }catch (FileException $e){
            }
            $duyurular->setFotograf($fileName);
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('duyurular_fotoekle', ['id' => $duyurular->getId(),]);
//        }
//        return $this->render('duyurular/fotoekle.html.twig', [
//            'duyurular' => $duyurular,
//            'form' => $form->createView(),
//            'id' => $duyurular->getId(),
//        ]);
    }

This is my .html.twig file/ form code

{% block body %}
    <h1>{{ duyurular.baslik }}</h1>
    <form action="{{ path('duyurular_fotoekle',{'id':id}) }}" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input name="duyuruid" value="{{ id }}" type="number" readonly>
        <input type="file" name="image" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    {% if duyurular.fotograf != null %}
       <img src="{{ asset('yuklenen') }}/fotograflar/{{ duyurular.fotograf }}" height="100">
    {% endif %}
{% endblock %}
  • 写回答

1条回答 默认 最新

  • doukang7486 2019-02-21 07:58
    关注

    I had to make a change on the controller. I added another function called 'iedit' than in the iedit.html.twig i called 'duyurular_fotoekle' . if you couldnt understand what i did dont hesitate to ask.

    /**
         * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"POST"})
         */
        public function fotoekle(Request $request, Duyurular $duyurular, $id): \Symfony\Component\HttpFoundation\RedirectResponse
        {
            $form = $this->createForm(DuyurularType::class, $duyurular);
            $form->handleRequest($request);
            echo ('sefa 2');
            $file = $request->files->get('image');
            // $file = $duyurular->getFotograf();
            // $file = $form->get('fotograf')->getData();
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
            try{
                $file->move(
                    $this->getParameter('fotograflar_directory'),
                    $fileName
                );
            }catch (FileException $e){
            }
            $duyurular->setFotograf($fileName);
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('duyurular_iedit', ['id' => $duyurular->getId(),]);
        }
    
        /**
         * @Route("/{id}/iedit", name="duyurular_iedit", methods="GET|POST")
         */
        public function iedit(Request $request,$id, Duyurular $duyurular,DuyurularRepository $duyurularRepository): Response
        {
            $duyurulist = $duyurularRepository ->findAll();
            $duyuruname = $duyurularRepository ->findBy(['id'=> $duyurular->getId()]);
            $form = $this->createForm(DuyurularType::class, $duyurular);
            $form->handleRequest($request);
    
            echo ('sefa 0');
            $this->getDoctrine()->getManager()->flush();
            if ($form->isSubmitted()) {
    
                echo ('sefa 1');
                return $this->redirectToRoute('duyurular_index', ['id' => $duyurular->getId()]);
            }
    
            return $this->render('duyurular/iedit.html.twig', [
                'duyurular' => $duyurular,
                'id'=>$id,
                'duyurulist' => $duyurulist,
                'duyuruname' => $duyuruname,
                'form' => $form->createView(),
            ]);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错