rji rji
rji rji

Reputation: 707

How to create a directory inside a directory in yii2 and upload file into that directory

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.

Upvotes: 3

Views: 13504

Answers (3)

Kartik Mohan
Kartik Mohan

Reputation: 1

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);

Upvotes: 0

Annie Chandel
Annie Chandel

Reputation: 207

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;
}

Upvotes: 3

Xiaosong Guo
Xiaosong Guo

Reputation: 495

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;
    }
}

Upvotes: 10

Related Questions