如何在yii2中的目录中创建目录并将文件上载到该目录中

时间:2016-04-13 08:00:00

标签: yii2 yii2-advanced-app yii2-basic-app yii2-model yii2-validation

我有一张表格

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

我正在上传表单中的文件并提交

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

在我的控制器中

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

这只是创建一个名为uploads的目录。但我想在每个用户的uploads目录中创建一个目录,即根据数据库表user中的主键创建并命名目录名。

例如,如果用户注册保存为主键 4 ,则必须创建名称为 4 的目录,并且必须将他上传的文件保存到该目录中。

如何实现这一目标?请帮忙。

3 个答案:

答案 0 :(得分:4)

在yii2中,您可以使用'yii \ helpers \ FileHelper'来创建文件夹。

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

在你的情况下:

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

答案 1 :(得分:2)

第1步:

在模型文件中使用'yii \ helpers \ FileHelper'。

第2步:

在要编写保存文件功能的模型文件中,添加以下行:

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

答案 2 :(得分:0)

以下是使用yii框架在另一个目录中创建目录并根据year-&gt; month-&gt; file_name存储文件的代码     年 - 父目录,     月 - 子目录

然后最终将文件存储在月目录中:

 $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);
相关问题