3

I have a form

  <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
 <?php echo $form->field($userformmodel, 'user_image')->fileInput(); ?>
 <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
 <?php ActiveForm::end(); ?>

I am uploading a file in the form and submit

In the model I have written the code as

 public function rules()
{
    return [
        [['user_image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg']
    ];
}

 public function upload()
{
    if ($this->validate()) {
        $this->user_image->saveAs('uploads/' . $this->user_image->baseName . '.' . $this->user_image->extension);
        return true;
    } else {
        return false;
    }
}

In my controller

 public function actionProfile()
{
 $model = new UserProfile();
 $userformmodel = new UserForm();
 $model->user_image = UploadedFile::getInstance($userformmodel, 'user_image');
 if($model->save(false))
                {
                $model->upload();        
                }
 }

This is just creating a directory called uploads. But I want to create a directory inside uploads directory for each user i.e, based upon the primary key in database table user I want to create and name the directory name.

Example, if the user registering is saved as primarykey 4, then a directory with name 4 must be created and the file he uploads must be saved into that directory.

How to make this happen? please help.

rji rji
  • 627
  • 2
  • 14
  • 34
  • Well, you should simply create dir before... What did you try ? – soju Apr 13 '16 at 08:05
  • I didn't try anything as I dint find any relative solution. How to create a sub directory – rji rji Apr 13 '16 at 08:36
  • Did you search ?? http://stackoverflow.com/questions/4384951/how-to-create-destination-folder-in-php-while-using-move-uploaded-file – soju Apr 13 '16 at 08:39
  • Is it the same in case of yii2? or do we have any widgets for the same? – rji rji Apr 13 '16 at 09:11
  • I would not recommend using the primary key for directory name (especially when the directory is publically accessible). create a unique token as directory name, create the directory with mkdir() (and chmod()) and upload your file to this destination. Store the directory name in database to link to user – MacGyer Apr 13 '16 at 21:27

3 Answers3

7

In yii2, you can use 'yii\helpers\FileHelper' to create folders.

FileHelper::createDirectory($path, $mode = 0775, $recursive = true);

In your case:

public function upload()
{
    if ($this->validate()) {
        $path = 'uploads/'. USERNAMEHERE .'/'. date('YMD');
        FileHelper::createDirectory($path);
        $this->user_image->saveAs($path .'/'. $this->user_image->baseName . '.' . $this->user_image->extension);
        return true;
    } else {
        return false;
    }
}
Xiaosong Guo
  • 465
  • 2
  • 7
3

Step 1:

Use 'yii\helpers\FileHelper' in your model file.

Step 2:

In the model file where you are writing the save file function, add these lines:

if ($this->validate()) {
    $path = 'uploads/'. USERNAMEHERE .'/'. date('YMD');
    FileHelper::createDirectory($path);
    $this->user_image->saveAs($path .'/'. $this->user_image->baseName . '.' . $this->user_image->extension);
    return true;
}
Pang
  • 8,605
  • 144
  • 77
  • 113
Annie Chandel
  • 197
  • 2
  • 3
0

Here is the code for creating directory inside another directory and storing the file according to year->month->file_name using yii framework year - parent directory, month - sub directory

Then finally storing the file in month directory:

 $new_file_name   = $_FILES['Book']['name']['image'];
            $path=YII::getPathOfAlias('webroot').'/resources/uploads/';
            $year_folder = $path . date("Y");
            $month_folder = $year_folder . '/' . date("m");


            // change umask to '0' by default to make folder with full permissions
            $oldmask = umask(0);

            /*
            for reference
            https://stackoverflow.com/questions/3997641/why-cant-php-create-a-directory-with-777-permissions
            */

            !file_exists($year_folder) && mkdir($year_folder , 0777);
            !file_exists($month_folder) && mkdir($month_folder, 0777);
            umask($oldmask);

            $path = $month_folder . '/' . $new_file_name;
            $res = move_uploaded_file ($_FILES['Book']['tmp_name']
                                                    ['image'],$path);
            chmod($path, 0666);
sniperd
  • 4,243
  • 6
  • 22
  • 36