aziz aziz -4 years ago 198
reST (reStructuredText) Question

Error: Call to a member function guessExtension() on null


how to edit this code for post file? Error: Error: Call to a member function guessExtension() on null (500 Internal Server Error) Code :


I try this:

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


Note: I'm using Advanced Rest Client

Answer Source
 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']);
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download