Reputation: 331
I have this form in my app, and I want to add an upload file field. I've been reading the docs, and when I test it, it doesn't upload the file. Nothing happens.
Here's the entity
/**
* @var string
*
* @ORM\Column(name="mynewpdf", type="string", length=350, nullable=true)
*/
private $mynewpdf;
/**
* Set mynewpdf
*
* @param string $mynewpdf
* @return userinfoid
*/
public function getmynewpdf()
{
return $this->mynewpdf;
}
/**
* Get mynewpdf
*
* @return string
*/
public function setmynewpdf($mynewpdf)
{
$this->mynewpdf = $mynewpdf;
return $this;
}
public function getPath()
{
$path = __DIR__.'/../../../../web/newfolder/';
$path .= '/'.$this->mynewpdf;
return $path;
} public function uploadmynewpdf($destinationDirectory)
{
if (null === $this->mynewpdf) return;
$nameFilePdf = uniqid().'.'.$this->mynewpdf->getClientOriginalExtension();
$this->_createdirectory($destinationDirectory);
$this->mynewpdf->move($destinationDirectory, $nameFilePdf);
$this->setmynewpdf($nameFilePdf);
}
private function _createdirectory($destinationDirectory)
{
$path = __DIR__.'/../../../../web/newfolder/';
if (!file_exists($path)) @mkdir($path, 0777, true);
$this->_createFileIndex($path);
if (!file_exists($destinationDirectory)) @mkdir($destinationDirectory, 0777, true);
$this->_createFileIndex($destinationDirectory);
}
private function _createFileIndex($myfolder)
{
$newfile = $myfolder.'index.html';
$content = "<html><head><title>403 Forbidden</title></head>".
"<body>This is Forbidden</body></html>";
if (!file_exists($newfile))
{
if (!$handle = @fopen($newfile, 'c')) die("Could not open/create new file");
if (@fwrite($handle, $content) === FALSE) die("Could not write the new file");
@fclose($handle);
}
return true;
}
My controller:
$file = $userinfoid->getMynewpdf();
if($file != null)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->container->getParameter('pdf_directory'),
$fileName
);
$userinfoid->setMynewpdf($fileName);}
And finally the form:
<form action="{{ path('add_document') }}" method="post" >
<input type="hidden" name="data[userinfoid]" value="{{ data.userinfoid }}" />
<div class="span12">
<label>Information about file</label>
<p>
<textarea id="fileinformation" rows="3" class="span11 campoInvalido" maxlength=""
title="Add some information"
name="data[fileinformation]" ></textarea>
</p>
</div>
<input type="file" id="mynewpdf" name="data[mynewpdf]" accept="application/pdf
</form>
I don't think the form was made like it was supposed to be made. But right now I cannot change it. Can someone tell me how can I make this work?
UPDATE:
First of all, don't forget to share key details about problems with your code.
This is an Ajax form. I never mentioned this, and should had.
My ajax request was like this:
$.ajax({
type:'POST',
url:'page/upload',
data: $("form").serialize(),
beforeSend:function(){$("#loadingModal").show();},
dataType:'json'})
The code above does not handle file uploads, since it serializes the data. So I replaced the code with this:
var formData = new FormData($("form")[0]);
$.ajax({
type:'POST',
url:'page/upload',
data: formData,
cache: false,
contentType: false,
processData: false,
beforeSend:function(){$("#loadingModal").show();},
dataType:'json'})
Then in my controller, I used this code:
$request = $this->getRequest();
$file = $request->files->get('mynewpdf');
// If a file was uploaded
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$userinfoid->setMynewpdf($filename);
$file->move(
$this->container->getParameter('pdf_directory'),
$filename
); // move the file to a path
}
Now I can save files in my web application!
Thanks Gabriel Diez for the help!
Upvotes: 2
Views: 70
Reputation: 1692
I think the major problem is your form. At the moment when you upload your file and send it to your action controller nothing is passed because your input type file isn't like symfony would like to have it. You should take a look at symfony docs on how to properly upload a file, it's very clear and complete and i think you missed some important details.
https://symfony.com/doc/2.8/controller/upload_file.html
And also for generate the form you should use the form helper from symfony it's the best and the fastest way to do it properly and safely.
https://symfony.com/doc/2.8/forms.html
If you look closer on the documentation you can see in the action controller this :
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
in your case Product would be your Entity.
and then $form is passed to the view.
In the method createForm you passed the Entity of the form so symfony knows what type of entity is passed in the form. So when it will build the form and the form will be submit it will associate the input file to your property $mynewpdf so then when you called :
$userinfoid->getMynewpdf();
Your file will be present and then you can manipulate it.
Good Luck
Upvotes: 1