Chapters
40 Chapters
|
4:39:58
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
05.
File Upload Field in a Form
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
This tutorial is built on Symfony 4 but works great in Symfony 5!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.87.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.9.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.0.3
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.2.3
"symfony/console": "^4.0", // v4.2.3
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.2.3
"symfony/framework-bundle": "^4.0", // v4.2.3
"symfony/property-access": "4.2.*", // v4.2.3
"symfony/property-info": "4.2.*", // v4.2.3
"symfony/security-bundle": "^4.0", // v4.2.3
"symfony/serializer": "4.2.*", // v4.2.3
"symfony/twig-bundle": "^4.0", // v4.2.3
"symfony/validator": "^4.0", // v4.2.3
"symfony/web-server-bundle": "^4.0", // v4.2.3
"symfony/yaml": "^4.0", // v4.2.3
"twig/extensions": "^1.5" // v1.5.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
"symfony/dotenv": "^4.0", // v4.2.3
"symfony/maker-bundle": "^1.0", // v1.11.3
"symfony/monolog-bundle": "^3.0", // v3.3.1
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
"symfony/stopwatch": "4.2.*", // v4.2.3
"symfony/var-dumper": "^3.3|^4.0", // v4.2.3
"symfony/web-profiler-bundle": "4.2.*" // v4.2.3
}
}
38 Comments
Hello, I have an issue with rendering the input file..Well the input file doesn't show, just the label appears.What is the problem?
Hey Cool,
What do you mean about "the input file doesn't show"? What browser do you use? Could you provide a screenshot maybe? It would help to understand the problem
Cheers!
Thank you victor for your response,
Well, the input file that rendered by the FileType in ArticleFormType does not appear. Although, it perfectly works..
I use Symfony4.3 and Google chrome as defaut browser. I even tried it in FireFox browser and the same problem remains.
I'm looking forward to your response.
Thanks.
The link of the problem's screenshot : https://i.imgur.com/fqIdXJS...
Hey Cool,
Hm, that's weird, I just double-checked with downloaded course code and I do have file path and even a "Browse" button to choose a file - I don't see the button on your screenshot as well. What is your OS? Windows? I'd recommend you to check your browser version, probably there might be some updates and you just need to install and reload your browser first. Also, make sure you don't have any extensions (most probably some add blockers etc.) that might block or modify your page content. Also, try to open this page in Chrome Incognito mode where all plugins are turned off by default.
If you still does not see it - most probably you have some style issues, it would be good to double check styles and HTML markup, you can debug things in Chrome with Chrome developer toolbar. Btw, did you download the course code for this tutorial and start from the start/ directory? Or did you start a completely separate fresh project? If the second, please, try to download the course code and bootstrap the project from the final/ directory. Did you see the file path and button there?
Anyway, it sounds more like browser problem, especially if it works perfectly but you can't see the path - that's not something you can tweak with styles or HTML code, it just works out of the box in browsers. So I mostly lean toward that some browser extension modifies the page behind the scene :/
I hope this helps!
Cheers!
Hello victor, I did desactivate all the extensions, my browser is in his latest version and I do even open the app in Chrome Incognito mode, but the problem is still remaining.
My PC's OS is Windows 8.1.
In fact, i did not download this couse code, I've started the project from Zero, step by step, from the first chapiter "Stellar Development with Symfony 4" to the Forms chapiter and continue with this course.
when I debug with Chrome developer toolbar, it sounds that this fiels has a style of "opacity:0", but when I make the "opacity:1", it gives me the standard file input not the bootsraped one.
This is the first issue I faced, and it sounds weird.
This is my Github project's link: https://github.com/mohamedT...
Thanks a lot for your help and I'm looking forward to your response again :)
Hey Cool,
Ah, I see... this really sounds like your project is missing some styles, actually that "Browse" button is added with CSS styles. It's difficult to say what exactly you're missing, I'd recommend you to download our course code and compare your HTML structure with the one we have in this screencast. Also keep an eye on styles you're including. Except of possible missing HTML/CSS in your project I find it difficult to say what exactly may cause this problem.
I hope this helps!
Cheers!
Hello victor ,
Thanks a lot for your help, I fixed the problem, I've just updated the bootstrap stylesheet in base.html.twig to the latest version 4.4.1 and the issue is fixed, it is as simple as that. :D
Hey Cool,
Ah, cool! :) Glad you was able to fix it! And thank you for sharing your solution.
Cheers!
Hi, I implements Upload File, and it works on http request, but when i use a domain and https request it fails and returns error 403.
Hey Jose,
Hm, I don't see any reasons why it should fail on HTTPS fairly speaking. Please, double-check our logs, there might be a tip why it fails. Also, make sure you send your request to the HTTPS and server works by HTTPS, i.e. can load pages in HTTPS.
I hope this helps!
Cheers!
hi, i'm using the FileType in symfony 5.3 but instead of an instance of UploadedFile i receive null. The others two fields works fine, i have problem only with file upload
Hey Alessandro,
Hm, if you get null instead of UploadedFile - most probably your file wasn't sent to the server. Are you sure selected and attached the file in your form? Are you sure the form is sent via POST instead of GET method? How do you render the form? Try to use special Twig functions to render form data, like form()/form_start()/form_end() instead of rendering the form tag manually, you may miss some important details when you render manually.
Cheers!
Yes to all but the effective difference Is the upload, i'm trying to use Ajax and It works for all param but not for files. If i use the normal submit works(i've tryed in the morning). I Always render the form with twig tag, it's so more clean and Easy
Hey Alessandro V.!
Hmmm. So it sounds like it might be an issue with your Ajax call not sending the file directly. What JavaScript are you using to submit the form via Ajax? If you open your network tools in your browser and find the Ajax request... then scroll down to the bottom where it says "Request body", what does it look like? Do you see plain-text fields that look like firstName=Ryan@lastName=Weaver ? Or do you see it structured as "multipart/form-data" (you can also know by looking at the headers on the request: there should be a Content-Type header set to "multipart/form-data".
I think you did a good job debugging by testing it with a normal (non-Ajax) submit. This definitely makes me think that we should look at the Ajax JavaScript first :).
Cheers!
Hey, i've tested without the ajax and the form works. https://pastebin.com/CQNX7625 this is the js code that i'm using. it serialize the content of the form. Maybe it can't serialize files
Hey Alessandro V.!
Yup, that's correct :). If you want to upload a file, you need to use the FormData object. Here's an example - https://stackoverflow.com/q... - the code is a little fancy (they created separate Upload object, which is not necessary), but hopefully you can see how they used FormData to attach the file, then used jQuery to upload that FormData.
Cheers!
How can I force a custom mime type for my file uploader? I am trying to upload .usdz files and the uploader converts them to .zip
If I use this:
'constraints' => [
new File([
'mimeTypes' => [
'model/vnd.usdz+zip',
'model/vnd.pixar.usd',
'model/usd',
'model/usdz'
],
'mimeTypesMessage' => 'Please upload a valid .usdz 3D file.',
])
]
in my FormType it will not let me upload the file, and if I do not use a constraint it will just convert it to .zip.
Thank you for the help.
Hey JavierMendezK !
I've answered this question over on the other thread :) https://symfonycasts.com/sc...
Cheers!
Hi,
Thank you for this awesome tutorial.
Where is the setImageFileName method defined? I dont have it and obviously my script runs into an "Attempted to call an undefined method..." error.
Please help.
Thank you
sorry. I have just found. It in the entity but mine is setPicutre, not setImageFileName. That was the problem....
Hey @ujjpeet!
I'm glad you worked it out :).
Cheers!
i have a specific question, i'm generating the controller thanks to make:controller, so it also make a new files in the folder src/form/entityNameType.php, but here is my issues, i did manualy deleted an entity i already made so i could use the maker again to have the matching form once i'll be in the /edit/ page, but still, when i'm tryint to re-make:entity, its tell me thats the previous entityNameFile.php is not found, this is due to a cache file it seems, so i tryed to purge it out, but that still doesnt work, here is the error message i've got when i try my make:crud :
`mChoose a name for your controller class (e.g. ?[39m?[33mArticleController?[39m?[32m)?[39m [?[33mArticleController?[39m]:
>
?[33mIn DebugClassLoader.php line 346:?[39m
?[37;41m
?[37;41m Warning: include(C:\wamp64\www\WorkPlace\Site_CETIS_Centre_Entrainement_Technique_Incendie_Survie\cetis_last\vendor\composer/../../src/Form/Ar
ticle ?[39;49m
?[37;41m 1Type.php): failed to open stream: No such file or directory
?[37;41m
?[32mmake:crud [?[39m?[32m<entity-class>?[39m?[32m]?[39m`
i dont understand this because yesterday this process was working as a charm... so i dont know what i did here, but a fix could be really helpful
Hey yacine,
I'm not sure if that's bug on the bundle but what you can do is to manually delete the cache folder (rm -rf var/cache) and try again, or remove all the files generated by the command and try again.
Cheers!
hello Diego
thanks for your reply, i just did delete the cache folder, then i launched make:crud, Then Article, and still getting the same error :/
for now i'm just dealing with the Article1Type with commenting it all, but thats not clean .
also, i already did try to remove all the files generated by the command, to be clear i'm deleting : ArticleController, the entire Article Folder in template, ArticleType.php (and Article1type), but the issue here is that somewhere in var\cache\dev\containerSOMEID -> getArticleControllerService.php there is a line that is callinig my controller class and this line is still here even when i did your rm -rf var\cache... i dont understand why :/ i already try to edit my ArticleController like this before and it worked perfectly..
Hey @yacine
Could you double check that all generated files were removed, and also, check for any code reference to any of the files, for example, any
usestatement of the entity.basicaly what i need here is to delete the Article1Type.php that war previously generated to only keep the ArticleType.php that was here at first.
(since i posted as a guest i cannot update my previous message to remove the part with the name of my project, if someone could errase this part from my previous message that would be perfect ♥ )
Whats Urlize?
It's a library for making a string URL compliant. For example, if your string has a slash
/in it, it will replace by a-Cheers!
Hi,
I am uploading a file via a form. The file(s) are images with OneToMany. The corresponding class is Images. When trying to use Articles::adImage() I get the error the passed argument is of type UploladedFile instead of Images but I have no idea what to do to meet the condition of Images!?
Thx
Oliver
Hey @Oliver!
Hmmm. It sounds possibly like the form fields might not be setup quite right for an embedded form - the images form field needs to be a collection of ImageFormType, not of FileType - I’m just guessing this is the issue.
Anyways, post some form code here and we’ll see if we can help :).
Cheers!
weaverryan
Hi Ryan,
sorry for the long timeout - I had to make some money ;-).
I've tryied to replace FileType::class by ImageFormType, but (of course?) this class does not exist. It seems as if I am still not on top of the mountain and further hints are welcome.
Thx
Hey Oliver W.!
> sorry for the long timeout - I had to make some money ;-).
I get that ;)
> I've tryied to replace FileType::class by ImageFormType, but (of course?) this class does not exist
Hmm. It should exist - I'm specifically talking about the FileType from Symfony - this one - https://symfony.com/doc/cur... - maybe check the use statement on that?
Cheers!
Hi Ryan,
it is still impossible to post an entire php-file. The systems cuts the leading lines!? Therefor find the files here: http://cloud.ad-libitum.inf...
Thx
Oliver
Hey Oliver W.!
It is possible, but it is not simple with the Disqus comments :/. Sorry about that!
Ok, thanks for posting the code! And I apologize for the confusion! I gave you (what I believe is) the correct advice initially, but it was not clear enough and then I actually got a bit confused in my replies. And also, now that I see the code, I can understand what you're doing a bit better!
To review, here is your setup:
1) On ArticleFormType, for the image field, you were originally setting this to a FileType from Symfony. Now that I see your setup, you SHOULD continue to do this again. You're setting this to mapped => false, which you must do for your setup, because you need to do extra processing before the data is ultimately set on Article.
2) Then, in ArticleAdminController, you are reading the UploadedFile objects off of the image field via
$uploadedFiles = $request->files->getIterator('image'). That's technically fine... but really, you should be able to read them off of the form with$form['image']->getData()(btw, I would probably call this field "images" since it is many images.3) Again in ArticleAdminController, you loop over
$uploadedFiles, which is an array of UploadedFile objects. This is good! And you build a$bildnamenarray where each item is eventually set onto$article->addImage($bild). Here is the problem. These$bildvariables are still UploadedFile objects. At some point, you need to create newImageobjects, set the filename on each, and set THAT onto$article->addImage().Anyways, another solution to all of this would be to build a form class for your Image entity - ImageFormType - and embed that into the ArticleFormType.image field. This would cause more things to happen automatically for you... but honestly, you already have things 95% working and I find your solution quite readable and you have a lot of control. So would continue to use an unmapped field like you are and get the last details smoothed out so it's working. And also, if you eventually find that users are needing to uploading multiple images, using a JavaScript uploader will get you a bit nicer UI :).
Cheers!
I've got it now. I fixed it using $bildobject = new Images(), $bildobjekt->setFilename() and $bildobjekt->setArticle() on the images-table. Not using articles.
Thx again
Hi weaverryan
Thx for your answer and as you can see I am even falling when making one step after the other. So guess what will happen when I take two steps at a time ;-). I will look at the JS-solution later. I've seen that video.
For the rest I have to wait for some free time to test your hints. I'll return to you - for shure!!
Oliver
How can I use this to upload several images in the same form? I am using Symfony 5.
Hey Allan,
Have you watched this tutorial to the end? :) We're talking about multiple files uploading further in https://symfonycasts.com/sc... . Also, we explain why multiple files uploading has bad UI e.g. if one of selected files does not pass validation - none files are saved. So, if you have not watched this course to the end - I'd recommend you to do this first, and I bet you will get answers to your questions :)
I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!