ok, here a little hack that i did because i couldn't get the tutorial one working on the following link
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html
with the following code
namespace app\controllers; use Yii; use yii\web\Controller; use app\models\UploadForm; use yii\web\UploadedFile; class SiteController extends Controller { public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); if ($model->upload()) { // file is uploaded successfully return; } } return $this->render('upload', ['model' => $model]); } }
Sadly, no matter how hard you try, $model->imageFile will always equal to null. If you are working on an Upload API, and you are trying to get your image or whatever upload. You might soon figure that this is an impossible task. But if you try to return $_FILES['imageFile'], it does show you some hot stuff which you have uploaded but the given code just doesn't work! Hence, I have a little hack to FORCE it to work for me at least.
By digging the code and logic within yii\web\UploadedFile i came down with the solution below,
namespace app\controllers; use Yii; use yii\web\Controller; use app\models\UploadForm; use yii\web\UploadedFile; class SiteController extends Controller { public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $file = $_FILES['imageFile']; $model->imageFile = new UploadedFile( [ 'name' => $file['name'], 'tempName' => $file['tmp_name'], 'type' => $file['type'], 'size' => $file['size'], 'error' => $file['error'], ]); if ($model->upload()) { // file is uploaded successfully return; } } return $this->render('upload', ['model' => $model]); } }
Its exactly the same as what is given just that i manually created the instances instead of using the method getInstance since getInstanceByName also doesn't work. Well, hopefully this help some poor folk out there. Good Luck!