I think you have to make sure the underlying structure also exists, so perhaps you should set the $recursive parameter of the mkdir to true. Also, you create a new instance of the model Task. If that model is an ActiveRecord model and the id parameter is your primary key, it will be empty unless you save it first (there will not be a Task ID)
(edit: I posted almost the same code as menxaca already mentioned, you probably have to save first or load your Task first.)
//$model = new Task(); //not this
//but this:
$model = Task::model()->findByPk($id);
//then $model->id will be available.
//or this:
$model = new Task();
//$model->id will be empty
$model->save();
//$model->id will be filled
You could use a transaction and roll it back if the upload failed, and you want to undo the saving of your model with :
$transaction = Yii::app()-db->beginTransaction();
try
{
//application logic (save model & store file, throw exception when it fails)
$transaction->commit() //will not be reached if exception occurs
} catch (Exception $e) {
$transaction->rollback();
}