move and rename file in symfony2 move and rename file in symfony2 symfony symfony

move and rename file in symfony2


The Symfony2 way of doing this would be similar to the following:

You can get uploaded files from the request object, by accessing the files property.Given you are in a controller context:

// get the request object$request = $this->get('request');// retrieve uploaded files$files = $request->files;// and store the file$uploadedFile = $files->get('archivo');$file = $uploadedFile->move($directory, $name);

By calling get on the FileBag object in $files, you get an instance of UploadedFile, which provides a method move, to store and rename the temporary file.

You get the error message because you're using an HTTP location instead of a filesystem path. Try to store the files somewhere in the web/ folder, for example:

$directory = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/imagenesportadas';