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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user