Yii2 Not allowed to load local resource (Image)

眉间皱痕 提交于 2019-12-25 18:48:07

问题


i got this error in inspect element under console

Not allowed to load local resource: file:///C:/xampp/htdocs/Yii2System/backend/web/data/cust_images/user_1.jpg

in view file:

 <div class="col-md-12 text-center">
            <?= Html::img($info->getUserPhoto($info->user_photo), ['alt'=>'No Image', 'class'=>'img-circle']); ?>
 </div>

in controller:

public function actionUserPhoto($uid)
{
    $model = $this->findModel($uid);
    $info = UserInfo::findOne($model->user_info_id);
    $info->scenario = 'photo-upload';
    $old_photo = $info->user_photo;

    if($info->load(Yii::$app->request->post()))
    {
        $info->attributes = $_POST['UserInfo'];
        $info->user_photo = UploadedFile::getInstance($info,'user_photo');

        if($info->validate('user_photo') && !empty($info->user_photo))
        {
            $ext= substr(strrchr($info->user_photo,'.'),1);
            $photo = $old_photo;
            $dir1 = Yii::getAlias('@backend').'/web/data/user_images/';

            if(file_exists($dir1.$photo) && $photo!='no-photo' && $photo!= NULL) {
                unlink($dir1.$photo);
            }
            if($ext!=null) {
                $newfname = $info->user_name.'_'.$info->user_id.'.'.$ext;
                $returnResults = $info->user_photo->saveAs(Yii::getAlias('@backend').'/web/data/user_images/'.$info->user_photo = $newfname);
                if($returnResults) {
                    $info->save(false);
                }
            }
        }
        return $this->redirect(['view', 'id' => $model->user_id]);
    }
    return $this->renderAjax('photo_form', ['model' => $model, 'info' => $info, ]);
}

who can help me to fix this problem...


回答1:


the browser don't load images off a local filesystem. You need to have it hosted and load it off a web server.

You can try solve this with this path

    file://C:/xampp/htdocs/Yii2System/backend/web/data

Otherwise .. place the image on an external (free) internet accessible server



来源:https://stackoverflow.com/questions/34685886/yii2-not-allowed-to-load-local-resource-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!