add Image and Upload helper classes
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
<?php
|
||||
namespace backend\controllers;
|
||||
|
||||
use yii\rest\ActiveController;
|
||||
use yii\rest\Controller;
|
||||
use common\models\Image;
|
||||
use backend\models\UploadForm;
|
||||
use yii\web\UploadedFile;
|
||||
USE Yii;
|
||||
|
||||
class UploadController extends Controller
|
||||
{
|
||||
@@ -19,28 +22,30 @@ class UploadController extends Controller
|
||||
|
||||
public function actionUpload()
|
||||
{
|
||||
$postdata = fopen( $_FILES[ 'data' ][ 'tmp_name' ], "r" );
|
||||
// /* Get file extension */
|
||||
$extension = substr( $_FILES[ 'data' ][ 'name' ], strrpos( $_FILES[ 'data' ][ 'name' ], '.' ) );
|
||||
$model = new UploadForm();
|
||||
$resp = [];
|
||||
|
||||
if ( $model->load(Yii::$app->request->post()) && $model->validate()) {
|
||||
|
||||
|
||||
$model->image = UploadedFile::getInstance($model, 'image');
|
||||
|
||||
/**save into frontend/web/uploads/profile*/
|
||||
$path = \common\components\Image::upload($model->image,'profile');
|
||||
|
||||
$image = new Image();
|
||||
$image->path = $path;
|
||||
$image->save();
|
||||
|
||||
/* the result object that is sent to client*/
|
||||
|
||||
// /* 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";
|
||||
$resp['ok'] = 1;
|
||||
$resp['id_image'] = $image->id_image;
|
||||
}else{
|
||||
$resp['ok'] = 0;
|
||||
$resp['message'] = print_r($model->errors,true);
|
||||
}
|
||||
|
||||
return $resp;
|
||||
}
|
||||
}
|
||||
@@ -3,22 +3,45 @@ namespace backend\models;
|
||||
|
||||
use yii\base\Model;
|
||||
use common\models\User;
|
||||
use common\models\Image;
|
||||
|
||||
class UploadForm extends Model{
|
||||
|
||||
|
||||
const SECRET= "aN6obLS2wMFzXw2VQBar";
|
||||
|
||||
public $image ;
|
||||
public $secret;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @formatter:off
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [['image',function ($attribute, $params) {}], ];
|
||||
return [
|
||||
|
||||
[['image'], 'image', 'mimeTypes' => 'image/jpeg, image/png', 'extensions'=>'jpg, png'],
|
||||
[['image'], 'required',],
|
||||
[['secret'], 'required'],
|
||||
[['secret'], 'validSecret'],
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
public function validSecret(){
|
||||
if ( $this->secret != self::SECRET ){
|
||||
$this->addError("secret","Invalid secret");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function save(){
|
||||
|
||||
$image = new Image();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use backend\models\UploadForm;
|
||||
|
||||
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
|
||||
|
||||
<?php echo $form->field($model, "image")->fileInput()?>
|
||||
|
||||
<div class="form-group">
|
||||
<?= Html::submitButton( Yii::t('common/ticket', 'Create') ,['class' => 'btn btn-primary']) ?>
|
||||
|
||||
Reference in New Issue
Block a user