Yii想要将文件从前端上传到yii样板中的后端/ www / images文件夹

Yii想要将文件从前端上传到yii样板中的后端/ www / images文件夹

问题描述:

Here is my code in frontend site controller to update user and i am adding user using backend so i store user images in backend/www/images folder and want to use same path for file upload when i upload user image from frontend My code in frontend is

public function actionupdateuser()
 {
     $model = User::model()->findByAttributes(array('memberid' => Yii::app()->user->id));
            if (isset($_POST['User']))
            {
                $rnd = rand(0, 9999);  // generate random number between 0-9999
                $uploadedFile = CUploadedFile::getInstance($model, 'photo');
                $fileName = "{$rnd}-00-{$uploadedFile}";  // random number + file name
                $model->attributes = $_POST['User'];

                if ($model->validate())
                {
                    if (!empty($uploadedFile))
                    {
     $path = Yii::app()->request->baseUrl . '/../../backend/www/images/';
                        $uploadedFile->saveAs($path . $fileName);
                        $model->photo = $fileName;
                    }
                    if ($model->save())
                        $this->redirect(array('useraccountdetail'));
                }
                else
                {
                    echo 'eroors';
                    exit;
                }
            }

            $this->render('updateuser', array('model' => $model));
        }

Here is the error i got

*move_uploaded_file(/simplifysupper/frontend/www/../../backend/www/images/6086-00-Penguins.jpg): failed to open stream: No such file or directory *

$basePath = str_replace(DIRECTORY_SEPARATOR.'protected', "", str_replace('frontend', '', Yii::app()->basePath));
$uploadDir = 'backend/www/images/';
$uploadedFile->saveAs($basePath .$uploadDir. $fileName);

Hope this helps ;)

You should specify full path to file (now you specify relative path) to file.

$pathToAppRoot = str_replace(DIRECTORY_SEPARATOR.'protected', "", Yii::app()->basePath);
$uploadedFile->saveAs(pathToAppRoot.'/simplifysupper/frontend/www/'.$fileName);