This article describes the YII2 advanced application of the custom components to achieve the global use of picture upload function method. Share to everyone for your reference, specific as follows:
This example is for the Yii2 High group application, where only a simple example is provided
In the YII2, when using to upload a picture, there is a upload picture class, but not very useful.
There is a way to write yourself a upload image class file, registered as a component, in the global use. (I remember having written a small object in it.)
Here, I'll just make a simple custom component introduction
1. Define a upload.php in backend (or frontend) (Note path: backend/components)
Image upload processing class (here slightly)
//upload.php case Code
namespace backend\components;
Class Upload
{public
function test () {
$a = ' hello ';
return $a;
}
2, backend/config/main.php registration just write custom components
' Components ' => ['
imgload ' => [
' class ' => ' Backend\components\upload '
],
]
3, how to use the controller
Public Function actionarticlelist ()
{
$cc = Yii:: $app->imgload->test ();
Var_dump ($CC); exit;
So, a complete picture upload component is not written here.
Only one way to use custom components is written here!
Let's just fill it in.
1, custom components, easy to use globally. backend/config/main.php
' Components ' => ['
user ' => [
' Identityclass ' => ' common\models\agmerch ',
' enableautologin ' = > True,
],
//Custom Picture upload class
' imgload ' => [
' class ' => ' Agent\components\upload '
],
]
2, custom picture upload class backend/components/upload.php
namespace Agent\components;
Use Yii;
Use Yii\base\object;
Use Yii\web\uploadedfile; Class Upload extends Object {/** * [uploadphoto description] * @param [type] $model [instantiated model] * @param [type] $path [picture Storage path] * @param [type] $originName [picture Source Name] * @param boolean $isthumb [whether to thumbnail]/public function up Loadphoto ($model, $path, $originName, $isthumb =false) {$root = $_server[' Document_root ']. '
/'. $path;
Returns an instantiated object $files = Uploadedfile::getinstance ($model, $originName); $folder = Date (' Ymd '). "
/";
$pre = rand (999,9999). Time (); if ($files && ($files->type = "Image/jpeg" | | $files->type = "Image/pjpeg" | | $files->type = = "Image/png " || $files->type = = "Image/x-png" | | $files->type = = "Image/gif")) {$newName = $pre. '. '
$files->getextension ();
}else{die ($files->type);
if ($files->size > 2000000) {die ("the uploaded file is too large"); } if (!is_dir ($root. $folder)) {if!mkdir ($root.$folder, 0777, True) {die (' Create directory failed ... ');
}else{//chmod ($root. $folder, 0777);
}}//echo $root. $folder. $newName; exit; if ($files->saveas ($root. $folder. $newName)) {if ($isthumb) {$this->thumbphoto ($files, $path. $folder.
$newName, $path. $folder. ' Thumb '. $newName); Return $path. $folder. $newName. '
# ' $path. $folder ' thumb '. $newName;
}else{return $path. $folder $newName;
}
}
}
}
3. Controller part
Use Backend\components\upload;
Public Function Actionartadd () {
$model = new Article ();
if ($model->load (yii:: $app->request->post ()) {
//cover for field names in table
$img = Yii:: $app->imgload-> Uploadphoto ($model, ' uploads/article/', ' cover ');
$model->cover = $img//To deposit in the table
if ($model->save ()) {
Yii:: $app->getsession ()->setflash (' Info ', ' Add success! ');
return $this->redirect ([' xxx ']);
} else{
Yii:: $app->getsession ()->setflash (' info ', ' Add failed! ');
@unlink ($img);
return $this->redirect ([' xxx ']);
}
Code slightly
}
4, view (part of the code)
<?= $form->field ($model, ' cover ', [
' Options ' =>[' class ' => '],
' inputoptions ' => [' class ' = > ' Form-control '],
]->fileinput ()->label (false);?>
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.