File Upload Symfony
I would like to upload multiple pictures in my form.
In fact, is working, but only with the small size of the picture ( like less than 1Mo). When I try to upload a picture with 4/5Mo, I have this error: The file "" does not exist
I noticed that it was due to the size because for small images it works very well.
So, my code :
Entity Picture who is related to Activity :
/**
* @ORMColumn(type="string")
* @AssertNotBlank(message="Please, upload the product brochure as a PNG, GIF, JPEG or JPG file.")
* @AssertFile(mimeTypes = {"image/png","image/jpeg","image/jpg","image/gif"})
* @AssertFile(maxSize="50M")
*/
private $url;
/**
* @ORMColumn(type="text", nullable=true)
*/
private $legende;
/**
* @ORMColumn(type="datetime")
*/
private $createdAt;
/**
* @ORMManyToOne(targetEntity="AppEntityActivity", inversedBy="pictures")
*/
private $activity;
The form pictureType :
->add('url', FileType::class, array('label' => 'Image (JPG, JPEG, PNG, GIF)'))
and the controller, in the create activity ( the picture form is here, when you add an activity, you can choose to add one or many pictures )
/**
* @Route("/create_activity", name="create_activity")
*/
public function create_activity(Request $request, ObjectManager $manager){
$activity = new Activity();
$form = $this->createForm(ActivityType::class, $activity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictures = $activity->getPictures(); // On récupère toutes les photos ajoutées
if($pictures){ // S'il y a des photos
foreach ($pictures as $picture ) { // Pour chaque photo
$file = $picture->getUrl(); // On récupère l'url uploadé
// ERROR COMING HERE at $file->guessExtension();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension(); // On génère un nom de fichier
// Move the file to the directory where brochures are stored
try {
$file->move( // On l'ajoute dans le répertoire
$this->getParameter('pictures_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$picture->setUrl($fileName);
}
}
$activity->setCreatedAt(new DateTime());
$activity->setDeleted(0);
$manager->persist($activity);
$manager->flush();
return $this->redirectToRoute('show_activity', ['id' => $activity->getId()]);
}
return $this->render('activity/create_activity.html.twig', [
'formActivity' => $form->createView(),
'activity' => $activity
]);
}
I made many 'echo; exit;' for know where the error is coming, and she comes from "$file->guessExtension();"
Thanks a lot for your help!
php file symfony upload symfony4
add a comment |
I would like to upload multiple pictures in my form.
In fact, is working, but only with the small size of the picture ( like less than 1Mo). When I try to upload a picture with 4/5Mo, I have this error: The file "" does not exist
I noticed that it was due to the size because for small images it works very well.
So, my code :
Entity Picture who is related to Activity :
/**
* @ORMColumn(type="string")
* @AssertNotBlank(message="Please, upload the product brochure as a PNG, GIF, JPEG or JPG file.")
* @AssertFile(mimeTypes = {"image/png","image/jpeg","image/jpg","image/gif"})
* @AssertFile(maxSize="50M")
*/
private $url;
/**
* @ORMColumn(type="text", nullable=true)
*/
private $legende;
/**
* @ORMColumn(type="datetime")
*/
private $createdAt;
/**
* @ORMManyToOne(targetEntity="AppEntityActivity", inversedBy="pictures")
*/
private $activity;
The form pictureType :
->add('url', FileType::class, array('label' => 'Image (JPG, JPEG, PNG, GIF)'))
and the controller, in the create activity ( the picture form is here, when you add an activity, you can choose to add one or many pictures )
/**
* @Route("/create_activity", name="create_activity")
*/
public function create_activity(Request $request, ObjectManager $manager){
$activity = new Activity();
$form = $this->createForm(ActivityType::class, $activity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictures = $activity->getPictures(); // On récupère toutes les photos ajoutées
if($pictures){ // S'il y a des photos
foreach ($pictures as $picture ) { // Pour chaque photo
$file = $picture->getUrl(); // On récupère l'url uploadé
// ERROR COMING HERE at $file->guessExtension();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension(); // On génère un nom de fichier
// Move the file to the directory where brochures are stored
try {
$file->move( // On l'ajoute dans le répertoire
$this->getParameter('pictures_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$picture->setUrl($fileName);
}
}
$activity->setCreatedAt(new DateTime());
$activity->setDeleted(0);
$manager->persist($activity);
$manager->flush();
return $this->redirectToRoute('show_activity', ['id' => $activity->getId()]);
}
return $this->render('activity/create_activity.html.twig', [
'formActivity' => $form->createView(),
'activity' => $activity
]);
}
I made many 'echo; exit;' for know where the error is coming, and she comes from "$file->guessExtension();"
Thanks a lot for your help!
php file symfony upload symfony4
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
1
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22
add a comment |
I would like to upload multiple pictures in my form.
In fact, is working, but only with the small size of the picture ( like less than 1Mo). When I try to upload a picture with 4/5Mo, I have this error: The file "" does not exist
I noticed that it was due to the size because for small images it works very well.
So, my code :
Entity Picture who is related to Activity :
/**
* @ORMColumn(type="string")
* @AssertNotBlank(message="Please, upload the product brochure as a PNG, GIF, JPEG or JPG file.")
* @AssertFile(mimeTypes = {"image/png","image/jpeg","image/jpg","image/gif"})
* @AssertFile(maxSize="50M")
*/
private $url;
/**
* @ORMColumn(type="text", nullable=true)
*/
private $legende;
/**
* @ORMColumn(type="datetime")
*/
private $createdAt;
/**
* @ORMManyToOne(targetEntity="AppEntityActivity", inversedBy="pictures")
*/
private $activity;
The form pictureType :
->add('url', FileType::class, array('label' => 'Image (JPG, JPEG, PNG, GIF)'))
and the controller, in the create activity ( the picture form is here, when you add an activity, you can choose to add one or many pictures )
/**
* @Route("/create_activity", name="create_activity")
*/
public function create_activity(Request $request, ObjectManager $manager){
$activity = new Activity();
$form = $this->createForm(ActivityType::class, $activity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictures = $activity->getPictures(); // On récupère toutes les photos ajoutées
if($pictures){ // S'il y a des photos
foreach ($pictures as $picture ) { // Pour chaque photo
$file = $picture->getUrl(); // On récupère l'url uploadé
// ERROR COMING HERE at $file->guessExtension();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension(); // On génère un nom de fichier
// Move the file to the directory where brochures are stored
try {
$file->move( // On l'ajoute dans le répertoire
$this->getParameter('pictures_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$picture->setUrl($fileName);
}
}
$activity->setCreatedAt(new DateTime());
$activity->setDeleted(0);
$manager->persist($activity);
$manager->flush();
return $this->redirectToRoute('show_activity', ['id' => $activity->getId()]);
}
return $this->render('activity/create_activity.html.twig', [
'formActivity' => $form->createView(),
'activity' => $activity
]);
}
I made many 'echo; exit;' for know where the error is coming, and she comes from "$file->guessExtension();"
Thanks a lot for your help!
php file symfony upload symfony4
I would like to upload multiple pictures in my form.
In fact, is working, but only with the small size of the picture ( like less than 1Mo). When I try to upload a picture with 4/5Mo, I have this error: The file "" does not exist
I noticed that it was due to the size because for small images it works very well.
So, my code :
Entity Picture who is related to Activity :
/**
* @ORMColumn(type="string")
* @AssertNotBlank(message="Please, upload the product brochure as a PNG, GIF, JPEG or JPG file.")
* @AssertFile(mimeTypes = {"image/png","image/jpeg","image/jpg","image/gif"})
* @AssertFile(maxSize="50M")
*/
private $url;
/**
* @ORMColumn(type="text", nullable=true)
*/
private $legende;
/**
* @ORMColumn(type="datetime")
*/
private $createdAt;
/**
* @ORMManyToOne(targetEntity="AppEntityActivity", inversedBy="pictures")
*/
private $activity;
The form pictureType :
->add('url', FileType::class, array('label' => 'Image (JPG, JPEG, PNG, GIF)'))
and the controller, in the create activity ( the picture form is here, when you add an activity, you can choose to add one or many pictures )
/**
* @Route("/create_activity", name="create_activity")
*/
public function create_activity(Request $request, ObjectManager $manager){
$activity = new Activity();
$form = $this->createForm(ActivityType::class, $activity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictures = $activity->getPictures(); // On récupère toutes les photos ajoutées
if($pictures){ // S'il y a des photos
foreach ($pictures as $picture ) { // Pour chaque photo
$file = $picture->getUrl(); // On récupère l'url uploadé
// ERROR COMING HERE at $file->guessExtension();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension(); // On génère un nom de fichier
// Move the file to the directory where brochures are stored
try {
$file->move( // On l'ajoute dans le répertoire
$this->getParameter('pictures_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$picture->setUrl($fileName);
}
}
$activity->setCreatedAt(new DateTime());
$activity->setDeleted(0);
$manager->persist($activity);
$manager->flush();
return $this->redirectToRoute('show_activity', ['id' => $activity->getId()]);
}
return $this->render('activity/create_activity.html.twig', [
'formActivity' => $form->createView(),
'activity' => $activity
]);
}
I made many 'echo; exit;' for know where the error is coming, and she comes from "$file->guessExtension();"
Thanks a lot for your help!
php file symfony upload symfony4
php file symfony upload symfony4
edited Nov 21 '18 at 16:55
Mehran
181311
181311
asked Nov 21 '18 at 16:07
LaiboaLaiboa
207
207
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
1
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22
add a comment |
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
1
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
1
1
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22
add a comment |
1 Answer
1
active
oldest
votes
If your $file
is an instance of UploadedFile
(which it should be in your case), you can use the method $file->getError()
to know if there was a problem during the upload of your file.
This method will return an integer
corresponding to this documentation : http://php.net/manual/en/features.file-upload.errors.php
In your case, i bet you will have a UPLOAD_ERR_INI_SIZE
error.
To solve this, you can see the solution here : Change the maximum upload file size
EDIT
Here's an example for you :
/** @var UploadedFile $file */
$file = $picture->getUrl();
if (UPLOAD_ERR_OK !== $file->getError()) {
throw new UploadException($file->getErrorMessage());
}
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416094%2ffile-upload-symfony%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your $file
is an instance of UploadedFile
(which it should be in your case), you can use the method $file->getError()
to know if there was a problem during the upload of your file.
This method will return an integer
corresponding to this documentation : http://php.net/manual/en/features.file-upload.errors.php
In your case, i bet you will have a UPLOAD_ERR_INI_SIZE
error.
To solve this, you can see the solution here : Change the maximum upload file size
EDIT
Here's an example for you :
/** @var UploadedFile $file */
$file = $picture->getUrl();
if (UPLOAD_ERR_OK !== $file->getError()) {
throw new UploadException($file->getErrorMessage());
}
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
add a comment |
If your $file
is an instance of UploadedFile
(which it should be in your case), you can use the method $file->getError()
to know if there was a problem during the upload of your file.
This method will return an integer
corresponding to this documentation : http://php.net/manual/en/features.file-upload.errors.php
In your case, i bet you will have a UPLOAD_ERR_INI_SIZE
error.
To solve this, you can see the solution here : Change the maximum upload file size
EDIT
Here's an example for you :
/** @var UploadedFile $file */
$file = $picture->getUrl();
if (UPLOAD_ERR_OK !== $file->getError()) {
throw new UploadException($file->getErrorMessage());
}
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
add a comment |
If your $file
is an instance of UploadedFile
(which it should be in your case), you can use the method $file->getError()
to know if there was a problem during the upload of your file.
This method will return an integer
corresponding to this documentation : http://php.net/manual/en/features.file-upload.errors.php
In your case, i bet you will have a UPLOAD_ERR_INI_SIZE
error.
To solve this, you can see the solution here : Change the maximum upload file size
EDIT
Here's an example for you :
/** @var UploadedFile $file */
$file = $picture->getUrl();
if (UPLOAD_ERR_OK !== $file->getError()) {
throw new UploadException($file->getErrorMessage());
}
If your $file
is an instance of UploadedFile
(which it should be in your case), you can use the method $file->getError()
to know if there was a problem during the upload of your file.
This method will return an integer
corresponding to this documentation : http://php.net/manual/en/features.file-upload.errors.php
In your case, i bet you will have a UPLOAD_ERR_INI_SIZE
error.
To solve this, you can see the solution here : Change the maximum upload file size
EDIT
Here's an example for you :
/** @var UploadedFile $file */
$file = $picture->getUrl();
if (UPLOAD_ERR_OK !== $file->getError()) {
throw new UploadException($file->getErrorMessage());
}
edited Nov 22 '18 at 8:39
answered Nov 22 '18 at 8:29
Alexandre BlondAlexandre Blond
513
513
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
add a comment |
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
It's working! Thanks a lot. I had update the wrong php.ini via wamp; So I went to the folder C:wamp64binphpphp7.2.11 (who is the good version.. ! ) and I update 3 files : php.ini php.ini-development and php.ini-production
– Laiboa
Nov 22 '18 at 15:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416094%2ffile-upload-symfony%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1) do not create your own bicycle - symfony.com/doc/master/bundles/EasyAdminBundle/integration/… :) 2) put an check that file exists: $file = $picture->getUrl(); if (null !== $file) { } 3) if it works with small files - maybe there is some limitation in php.ini - stackoverflow.com/questions/2184513/…
– myxaxa
Nov 21 '18 at 17:22
Thanks but I have already check the php.ini and it's good.. I will try the bundle, but I don't understand why it's not working like I do :/ The file exist if she is small but if she is big, she don't exist..
– Laiboa
Nov 21 '18 at 21:37
1
there is also post_max_size and upload_max_filesize - are you sure that they are set correctly?
– myxaxa
Nov 22 '18 at 8:22