0

i'm using symfony 2.5 & Php 5.3 ( old server )

i just want to add an input file in my form to display his pathname like (C://pathto/filename)

I've added an attribute in my class Advert.php like that :

  /**
 * @ORM\Column(type="string", nullable=true)
 *
 */
private $brochure = null;

public function getBrochure()
{
    return $this->brochure;
}

public function setBrochure($brochure)
{
    $this->brochure = $brochure;

    return $this;
}

and in my AdvertType.php :

            ->add('brochure', 'file', array(
            'required'   => false,))

But i think i'm missing something in my addAction because it returns me a bad path like : (/tmp/phpYAVwMQ) instead of (C://filename.pdf) when displaying {{ advert.brochure }}

    public function addAction(Request $request)
{
    $advert = new Advert();
    $form = $this->createForm(new AdvertType(), $advert);
    $usr = $this->get('security.context')->getToken()->getUser();

    if ($form->handleRequest($request)->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $advert->setAuthor($usr->getUsername());
        $advert->setDate(new \DateTime);
        $em->persist($advert);
        $em->flush();
        $request->getSession()->getFlashBag()->add('info', 'Annonce bien enregistrée.');

        // On redirige vers la page de visualisation de l'annonce nouvellement créée/
        return $this->redirect($this->generateUrl('info_view', array('id' => $advert->getId())));
    }

    return $this->render('SocietyPerfclientBundle:Default:add.html.twig', array(
        'form' => $form->createView(),
    ));
}

Please help me :)

2 Answers 2

1

You can't get full path of local machine ("C:\pathto"), just the filename, for security reasons
Add this line after your $advert->setDate line :

$advert->setBrochure($form->get('brochure')->getData()->getClientOriginalName());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Luax, you're the best ! <3
0

The Symfony 'file' input type is for a file upload. The path that it gives you if the location on your webserver that the file has been put, for now.

Documentation on using the unmaintained Symfony v2.5 is available, with examples on what information is available about the uploaded file, including the original file name.

Updating the project to at least the Long-Term-Support version 2.7 should be early on the list of things to do, and then to 3.3+, via 2.8, and 3.0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.