I'm using following code to upload a file submitted through a form. How can I get file size before uploading it? I want the max file size to be 20mb.
$file = $data->getFileName();
if ($file instanceof UploadedFile) {
$uploadManager = $this->get('probus_upload.upload_manager.user_files');
if ($newFileName = $uploadManager->move($file)) {
$data->setFileName(basename($newFileName));
}
}
Oldskool is correct. However if you want to retrieve the exact file size after it has been uploaded, you can use the following:
$fileSize = $file->getClientSize();
Another solution would be to change maximum size of upload file in php.ini. The following will echo the current file size limit.
echo $file->getMaxFilesize();
To get form errors, you should validate the form and print any errors if the validation fails.
//include at the top of controller
use Symfony\Component\HttpFoundation\Response;
$form = $this->createForm(new FileType(), $file);
$form->handleRequest($request);
if ($form->isValid()) {
//store data
$data = "stored successfully";
$statusCode = 200;
} else {
//return errors if form is invalid
$data = $form->getErrors();
$statusCode = 500;
}
return new Response($data, $statusCode);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With