【问题标题】:Yii framework failed to open streamYii 框架无法打开流
【发布时间】:2012-03-01 13:48:42
【问题描述】:

我正在使用 Yii 框架,我想创建用户注册页面。一切似乎都正常,但是当我想上传用户图片时,我收到一条错误消息:

include(Self.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

这是我的 SiteController 代码:

public function actionRegister()
{
    $model=new RegisterForm;

    // uncomment the following code to enable ajax-based validation

    if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    if(isset($_POST['RegisterForm']))
    {
        $model->attributes=$_POST['RegisterForm'];
        if($model->validate())
        {
            $user = new User;
            $user->username = $_POST['RegisterForm']['username'];
            $password = $_POST['RegisterForm']['password'];
            $salt = Security::GenerateSalt(128);
            $user->password = hash('sha512', $password.$salt);
            $user->question = $_POST['RegisterForm']['question'];
            $user->answer = $_POST['RegisterForm']['answer'];
            $user->salt = $salt;
            $user->email = $_POST['RegisterForm']['email'];
            $user->fullname = $_POST['RegisterForm']['fullname'];
            $user->birth = $_POST['RegisterForm']['birth'];
            $user->avatar = CUploadedFile::getInstance($model,'image');
            $user->avatar->saveAs('/images/users.test.jpg');
            $user->about = $_POST['RegisterForm']['about'];
            $user->signup = date('d/m/y');
            $user->login = date('d/m/y');
            $user->save();
        }
    }
    $this->render('register',array('model'=>$model));
}

这是我的 RegisterForm 模型代码:

<?php
class RegisterForm extends CFormModel
{
[.....]

 public $avatar;

[.....]

    public function rules()
    {
        return array( 

[......]

            array('avatar', 'file', 
                                'types'=>'jpg, gif, png', 
                                'maxSize'=>1024 * 1024 * 2,
                                'tooLarge'=>'The file was larger than 2MB. Please upload a smaller file.',
                                'wrongType'=>'Please upload only images in the format jpg, gif, png',
                                'tooMany'=>'You can upload only 1 avatar',
                        'on'=>'upload'),

[......]

}

观点:

<div class="row">
    <?php echo $form->labelEx($model,'avatar'); ?>
    <?php echo CHtml::activeFileField($model, 'avatar'); ?>
    <?php echo $form->error($model,'avatar'); ?>
</div>

【问题讨论】:

    标签: php file file-upload frameworks yii


    【解决方案1】:

    可能是目录权限问题。要确定是否是,请执行

    chmod 0777 <dir_name>
    

    确定后,即可正确重置权限。

    【讨论】:

    • 不,我发现了一个简单的问题:我正在使用静态函数,并且我在这样的类中调用 >> self::testFunction();好像不正确,那么它的正确形式是怎样的呢?
    • 假设类名是A,那么可以用类名-A::testFunction()调用静态函数
    • 但是如果静态函数在 A 中并且我想从 A 调用 if ? $this->testFunction();不工作
    • 在同一个类中,self::staticFunction() 应该可以工作。为什么这会给你一个“无法打开目录”的错误?
    • 哦,我傻了,我用的是 Self:staticFunction() 而不是 selft:staticFunction 我忘了它是区分大小写的
    猜你喜欢
    • 2013-02-10
    • 2013-04-20
    • 1970-01-01
    • 2014-04-16
    • 2023-01-20
    • 2023-03-13
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多