Editing Symfony2 file upload without permissions

Please help me because I cannot believe my eyes.

I refuse to use a third-party plugin to upload files and refuse to create a separate object for the file / document. I just need a simple file upload that I would do in Zend / Laravel, etc.

I have a table of accounts with the last attachment name “attachment”, I want to save my disinfected name here (for example: 123421_filename.jpg), the upload form and the download went well.

the code is here:

//AddAction
     $file=$form['attachment']->getData();
        $fileName=$file->getClientOriginalName();
        $sanitizedFilename=rand(1, 99999).'_'.$fileName;
        $dir='files/'.$userId.'/';
        $file->move($dir, $sanitizedFilename);
    ...
        $invoice->setAttachment($sanitizedFilename);
        $em = $this->getDoctrine()->getManager();
        $em->persist($invoice);

The first problem I encountered, and don’t know how to solve it, was to use the edit form. I have a formbuilder

   $builder->add('attachment', 'file',array('data_class'=>null))

"" , , , data_class= > null.., , , - , NULL, , .

$builder->add('attachment', 'text')

, - ... ? Document?!

( , , laravel, " ". symfony , .. , , , ? , , ?)

, , ( , , $builder- > add ('attachment', file ', array (' data_class '= > null ))

, ?

public function editAction($id)
   ....
   $invoice = $em->getRepository('AppBundle:Invoice')->find($id);
   ..
    if ($form->isValid()) {

            $oldFileName=$invoice->getAttachment();  //this is null
            $oldFileName=$invoice->getId();   //this returns the invoice id
            $oldFileName=$invoice->getValue(); //returns invoice value

                echo 'old: '.$oldFileName.'<br/>';
                exit();
     }

, -, , , ? ?

, , -, $invoice, -

 if ($form->isValid()) {
    $sameInvoice= $em->getRepository('AppBundle:Invoice')->find(20); //hardcoded ID

                   $oldFileName=$sameInvoice->getAttachment();   //still null
                   $oldFileName=$sameInvoice->getId();   //returns 20
                    echo 'old: '.$oldFileName.'<br/>';
                    exit();

, , , , , .. ?

? ( ?.. , , ... "" ). " t invoice- > , , , , ?!

enter image description here

+4
2

, :

, , NULL, , $invoice :

$invoice = $em->getRepository('AppBundle:Invoice')->find($id);
$form = $this->createForm(new InvoiceType($id), $invoice);

: $invoice- > getAttachment();

, , , : "getClientOriginalName()" , .

, , $invoice,

    $invoice = $em->getRepository('AppBundle:Invoice')->find($id);
    $oldFileName=$invoice->getAttachment();  //this is stil the object here I can see the record from the database
    $form = $this->createForm(new InvoiceType($id), $invoice);
  //here the $invoice->getAttachment(); returns null

, , :

 if ($form->isValid()) {

                $fs=new Filesystem();
                $newFile=$form['attachment']->getData();
                $newFileName=$newFile->getClientOriginalName();

                if($newFileName != $oldFileName)
                {
                  //  if($fs->exists('files/'.$this->getUser()->getId().'/'.$oldFileName))
                  //      $fs->remove('files/'.$this->getUser()->getId().'/'.$oldFileName);

                    $sanitizedFilename=rand(1, 99999).'_'.$newFileName;
                    $dir='files/'.$this->getUser()->getId().'/';
                    $newFile->move($dir, $sanitizedFilename);
                    $invoice->setAttachment($sanitizedFilename);
                }

                $em->persist($invoice);
                $em->flush();

                return $this->redirect($this->generateUrl('my-invoices'));
            }

, -, :

return $this->render('AppBundle:Invoices:edit.html.twig', array(
            'oldFileName' => $oldFileName) 

, twig

0

. , .

myControllerAction()
{
    // No need for an object, an array works fine
    $model = array(
        'invoice' => $invoice,
        'attachment' => null
    );

    $builder = $this->createFormBuilder($model);

    $builder->setAction($this->generateUrl('cerad_game_schedule_import'));
    $builder->setMethod('POST');

    $builder->add('attachment', 'file');
    $builder->add('invoice', new InvoiceType());

    $builder->add('import', 'submit', array(
        'label' => 'Import From File',
        'attr' => array('class' => 'import'),
    ));        
    $form = $builder->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {   
        $model = $form->getData();
        $file = $model['attachment']; // The file object is built by the form processor

        if (!$file->isValid())
        {
            $model['results'] = sprintf("Max file size %d %d Valid: %d, Error: %d<br />\n",
            $file->getMaxFilesize(), // Returns null?
            $file->getClientSize(),
            $file->isValid(), 
            $file->getError());
            die($model['results'];
        }
        $importFilePath = $file->getPathname();
        $clientFileName = $file->getClientOriginalName();

        // Xfer info to the invoice object and update

    }
0

All Articles