{"id":7899,"date":"2015-11-07T04:57:55","date_gmt":"2015-11-07T04:57:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/07\/how-to-upload-a-file-to-directory-in-yii2-open-source-projects-yiisoft-yii2\/"},"modified":"2015-11-07T04:57:55","modified_gmt":"2015-11-07T04:57:55","slug":"how-to-upload-a-file-to-directory-in-yii2-open-source-projects-yiisoft-yii2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/07\/how-to-upload-a-file-to-directory-in-yii2-open-source-projects-yiisoft-yii2\/","title":{"rendered":"How to upload a file to directory in yii2?-open source projects yiisoft\/yii2"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/2d25ed050467646101881571fa96f33e?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>Devy<\/strong><\/p>\n<p>Follow the official documentation<\/p>\n<p>https:\/\/github.com\/yiisoft\/yii2\/blob\/master\/docs\/guide\/input-file-upload.md<\/p>\n<p>Form Model<\/p>\n<pre><code>namespace app\\models;\n\nuse yii\\base\\Model;\nuse yii\\web\\UploadedFile;\n\n\/**\n* UploadForm is the model behind the upload form.\n*\/\nclass UploadForm extends Model\n{\n\/**\n * @var UploadedFile|Null file attribute\n *\/\npublic $file;\n\n\/**\n * @return array the validation rules.\n *\/\npublic function rules()\n{\n    return [\n        [['file'], 'file'],\n    ];\n}\n}\n?&gt;\n<\/code><\/pre>\n<p>Form View<\/p>\n<pre><code>\n\n\n\nSubmit\n\n\n<\/code><\/pre>\n<p>Controller<\/p>\n<p>Now create the controller that connects form and model together:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>Instead of <code>model-&gt;load(...)<\/code> we are using <code>UploadedFile::getInstance(...)<\/code>. <code>[[\\yii\\web\\UploadedFile|UploadedFile]]<\/code> does not run the model validation. It only provides information about the uploaded file. Therefore, you need to run validation manually via <code>$model-&gt;validate()<\/code>. This triggers the <code>[[yii\\validators\\FileValidator|FileValidator]]<\/code> that expects a file:<\/p>\n<pre><code> $file instanceof UploadedFile || $file-&gt;error == UPLOAD_ERR_NO_FILE \/\/in code framework\n<\/code><\/pre>\n<p>If validation is successful, then we&#8217;re saving the file:<\/p>\n<pre><code> $model-&gt;file-&gt;saveAs('uploads\/' . $model-&gt;file-&gt;baseName . '.' . $model-&gt;file-&gt;extension);\n<\/code><\/pre>\n<p>If you&#8217;re using &#8220;basic&#8221; application template then folder uploads should be created under web.<\/p>\n<p>That&#8217;s it. Load the page and try uploading. Uplaods should end up in basic\/web\/uploads.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Devy Follow the official documentation https:\/\/github.com\/yiisoft\/yii2\/blob\/master\/docs\/guide\/input-file-upload.md Form Model namespace app\\models; use yii\\base\\Model; use yii\\web\\UploadedFile; \/** * UploadForm is the model behind the upload form. *\/ class UploadForm extends Model { \/** * @var UploadedFile|Null file attribute *\/ public $file; \/** * @return array the validation rules. *\/ public function rules() { return [ [[&#8216;file&#8217;], &#8216;file&#8217;], [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7899","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7899"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7899\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}