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.

5
  • Well, you should simply create dir before... What did you try ? Commented Apr 13, 2016 at 8:05
  • I didn't try anything as I dint find any relative solution. How to create a sub directory Commented Apr 13, 2016 at 8:36
  • Did you search ?? stackoverflow.com/questions/4384951/… Commented Apr 13, 2016 at 8:39
  • Is it the same in case of yii2? or do we have any widgets for the same? Commented Apr 13, 2016 at 9: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 Commented Apr 13, 2016 at 21:27

3 Answers 3

10

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.