add UploadController 'rest' controller
This commit is contained in:
@@ -6,6 +6,7 @@ use yii\filters\AccessControl;
|
||||
use yii\web\Controller;
|
||||
use common\models\LoginForm;
|
||||
use yii\filters\VerbFilter;
|
||||
use backend\models\UploadForm;
|
||||
|
||||
/**
|
||||
* Site controller
|
||||
@@ -22,7 +23,7 @@ class SiteController extends Controller
|
||||
'class' => AccessControl::className(),
|
||||
'rules' => [
|
||||
[
|
||||
'actions' => ['login', 'error'],
|
||||
'actions' => ['login', 'error','upload-image'],
|
||||
'allow' => true,
|
||||
],
|
||||
[
|
||||
@@ -55,7 +56,6 @@ class SiteController extends Controller
|
||||
|
||||
public function actionIndex()
|
||||
{
|
||||
Yii::$app->security->generatePasswordHash('test');
|
||||
return $this->render('index');
|
||||
}
|
||||
|
||||
@@ -85,4 +85,11 @@ class SiteController extends Controller
|
||||
|
||||
return $this->goHome();
|
||||
}
|
||||
|
||||
public function actionUploadImage(){
|
||||
\yii::$app->request->enableCsrfValidation = false;
|
||||
$model = new UploadForm();
|
||||
|
||||
return $this->render('upload', ['model' =>$model]);
|
||||
}
|
||||
}
|
||||
|
||||
46
backend/controllers/UploadController.php
Normal file
46
backend/controllers/UploadController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace backend\controllers;
|
||||
|
||||
use yii\rest\ActiveController;
|
||||
use yii\rest\Controller;
|
||||
|
||||
class UploadController extends Controller
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// public function verbs()
|
||||
// {
|
||||
// $verbs = [];
|
||||
// $verbs[ "upload" ] = ['POST' ];
|
||||
// return $verbs;
|
||||
// }
|
||||
|
||||
public function actionUpload()
|
||||
{
|
||||
$postdata = fopen( $_FILES[ 'data' ][ 'tmp_name' ], "r" );
|
||||
// /* Get file extension */
|
||||
$extension = substr( $_FILES[ 'data' ][ 'name' ], strrpos( $_FILES[ 'data' ][ 'name' ], '.' ) );
|
||||
|
||||
// /* Generate unique name */
|
||||
$filename = \Yii::getAlias('@frontend') ."/web/profile/" . uniqid() . $extension;
|
||||
echo $filename;
|
||||
|
||||
// /* Open a file for writing */
|
||||
$fp = fopen( $filename, "w" );
|
||||
|
||||
/* Read the data 1 KB at a time
|
||||
and write to the file */
|
||||
while( $data = fread( $postdata, 1024 ) )
|
||||
fwrite( $fp, $data );
|
||||
|
||||
/* Close the streams */
|
||||
fclose( $fp );
|
||||
fclose( $postdata );
|
||||
|
||||
/* the result object that is sent to client*/
|
||||
|
||||
echo "ok";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user