2017-03-19 23 views
0

how to edit this code for post file? Error: Error: Call to a member function guessExtension() on null (500 Internal Server Error) Code :エラー:ヌルにメンバ関数guessExtension()の呼び出し

は、私はこれを試してみてください。

public function ekleAction(Request $request) 
    { 

    //doctrini çağırdık 
    $em=$this->getDoctrine()->getManager(); 

    //posttan gelen veriler 
    $adi=$request->get('adi'); 
    $aciklama=$request->get('aciklama'); 
    $fiyat=$request->get('fiyat'); 
    $fotom = $request->files->get('foto'); 
    $kapak_foto=$request->files->get('kapakFoto'); 
    $kategorim=$request->get('kategori'); 
    $telefon=$request->get('usrtel'); 
    $user=$request->get('uye_id'); 

    $kategori= $em->getRepository("VanBundle:Kategori")->findOneBy(array('id'=>$kategorim)); 
    $user2=$em->getRepository("VanBundle:User")->findOneBy(array('id'=>$user)); 
    $serializer=$this->get('jms_serializer'); 

    try { 
     $oto = new Oto(); 
     $oto->setAdi($adi); 
     $oto->setAciklama($aciklama); 
     $oto->setFiyat($fiyat); 
     $oto->setKategori($kategori); 
     $oto->setUye($user2); 


     if ($telefon == null) { 
      $oto->setTelefon("yok"); 
     } else { 
      $oto->setTelefon($telefon); 
     } 


     $fileName2 = md5(uniqid()) . '.' . $kapak_foto->guessExtension(); 

     $kapak_foto->move(
      $this->getParameter('brochures_directory'), 
      $fileName2 
     ); 

     $oto->setKapakFoto($fileName2); 


     $images = array(); 
     if ($fotom != null) { 
      $key = 0; 


      // Çoklu Fotoğraf alma 
      foreach ($fotom as $file) { 
       $fileName = md5(uniqid()) . '.' . $file->guessExtension(); 

       $file->move(
        $this->getParameter('brochures_directory'), 
        $fileName 
       ); 
       $images[$key++] = $fileName; 


       $foto = new Foto(); 
       $oto->addFotolar($foto); 
       $foto->setAdi($fileName); 
       $foto->setOto($oto); 

       foreach ($images as $uploadfileName) { 


        $em->persist($oto); 
        $em->persist($foto); 
        $em->flush(); 

       } 
      } 
     } 

     $data=$serializer->serialize("Başarılı",'json'); 

     return new Response($data,200,['content-type'=>'application/json']); 

    }catch (Exception $exception){ 

     $data=$serializer->serialize($exception->getMessage(),'json'); 

     return new Response($data,200,['content-type'=>'application/json']); 
    } 
} 

$kapak_foto=$request->files->get('kapakFoto'); //null 

注:私は高度な休息クライアント

+0

あなたのコントローラに送信され、元の要求がどのようなものが見えますか? symfonyの 'Request'クラスの' FileBag'はPHPの '$ _FILES'スーパーグローバルの内容に基づいて公開されています。要求が 'multipart/form-data'のタイプのものであれば、適切な送信データのみが入力されます(http://php.net/manual/en/features.file-upload.post-method.phpを参照) 。 – xabbuh

答えて

-1
public function ekleAction(Request $request) 
    { 
     //doctrini çağırdık 
     $em=$this->getDoctrine()->getManager(); 





     //posttan gelen veriler 

     $data = json_decode($request->get('formData'),true); 


     $adi=$data['adi']; 
     $aciklama=$data['aciklama']; 
     $fiyat=$data['fiyat']; 
     $fotom = $request->files->get('foto'); 
     $kapak_foto=$request->files->get('kapakFoto'); 
     $kategorim=$data['kategori']; 
     $telefon=$data['usrtel']; 
     $user=$data['uye_id']; 



     $kategori= $em->getRepository("VanBundle:Kategori")->findOneBy(array('id'=>$kategorim)); 
     $user2=$em->getRepository("VanBundle:User")->findOneBy(array('id'=>$user)); 
     $serializer=$this->get('jms_serializer'); 



     try { 
      $emlak = new Emlak(); 
      $emlak->setAdi($adi); 
      $emlak->setAciklama($aciklama); 
      $emlak->setFiyat($fiyat); 
      $emlak->setKategori($kategori); 
      $emlak->setUye($user2); 


      if ($telefon == null) { 
       $emlak->setTelefon("yok"); 
      } else { 
       $emlak->setTelefon($telefon); 
      } 


      $fileName2 = md5(uniqid()) . '.' . $kapak_foto->guessExtension(); 

      $kapak_foto->move(
       $this->getParameter('brochures_directory'), 
       $fileName2 
      ); 

      $emlak->setKapakFoto($fileName2); 
      $images = array(); 
      if ($fotom != null) { 
       $key = 0; 


       //burası şuani için array değil 
       // Çoklu Fotoğraf alma 
       foreach ($fotom as $file) { 
        // $file = $fotom; 
        $fileName = md5(uniqid()) . '.' . $file->guessExtension(); 

        $file->move(
         $this->getParameter('brochures_directory'), 
         $fileName 
        ); 
        $images[$key++] = $fileName; 


        $foto = new Foto(); 
        $emlak->addFotolar($foto); 
        $foto->setAdi($fileName); 
        $foto->setEmlak($emlak); 

        foreach ($images as $uploadfileName) { 


         $em->persist($emlak); 
         $em->persist($foto); 
         $em->flush(); 

        } 
       } 
      } 

      $data=$serializer->serialize("Başarılı",'json'); 

      return new Response($data,200,['content-type'=>'application/json']); 

     }catch (Exception $exception){ 

      $data=$serializer->serialize($exception->getMessage(),'json'); 

      return new Response($data,200,['content-type'=>'application/json']); 
     } 
    } 
-1

は、エラーメッセージがある使用していますあなたに問題があることを伝え、違反行を特定したことさえあります。あなたはあなたのコードを壊すことを避けるために、いくつかのチェックを追加する必要があります

if ($kapak_foto instanceof WhateverObject) { 
    $fileName2 = md5(uniqid()) . '.' . $kapak_foto->guessExtension(); 

    // ... 
} 

、あなたはすなわち、問題の原因を解決

$kapak_foto=$request->files->get('kapakFoto'); //null 

が正しいオブジェクトを返して、あなたのラインを確認してください。

+0

kapak写真をカバーすることを意味します。私は$ kapak_foto instanceofファイルを試します。 動作しません。私はあなたの助けが必要です – aziz

+0

ステートメントがFile型のオブジェクトを生成する方法はありません。必要なものを実現するためにドキュメントに従ってみてください:http://symfony.com/doc/master/controller/upload_file.html symfonyフォームを使用する場合、symfonyがあなたのために行ういくつかの作業を手動で行っています。 – ehymel

+0

symfonyのフォームを使用したくありません。 – aziz

0

私はこのコードを試してみてください。

public function indexAction(Request $request) 
    { 
     $foto=$request->files->get('foto'); 




     return new Response(var_dump(base64_decode($foto))); 
    } 

結果:文字列(0) ""

関連する問題