yii2.0, the single image uploaded to the Sohu ptz and the picture uploaded to the Computer.
Last Update:2016-08-04
Source: Internet
Author: User
<span id="Label3"></p><p><p>The image server is the Sohu cloud Platform. On the Sohu cloud platform has the code package, downloads down, puts in the Yii frame Vendor.</p></p><p><p>yii2.0 Import a third-party library, it's simple, write a autoload file, and then include the AutoLoad file in the portal script Index.php. Specific to this cloud platform library, as long as the code contains the AutoLoad file is Good.</p></p><p><p>Brief introduction of the implementation of the Sohu ptz, the user will have a corresponding domain name, and then choose to build some buckets, file upload download is carried out in this bucket. function calls are clearly described in the documentation provided by the Gimbal. This is no longer introduced Here. At the same time I only from the PHP section to introduce, regardless of the front-end Section.</p></p><p><p>Controller section</p></p><pre><pre><span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">controller{ </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">function</span> <span style="color: #000000;"> actionupload () { </span><span style="color: #008000;">//</span><span style="color: #008000;">return Imgupdate::uploadtoscs ($_files);</span> <span style="color: #0000ff;">return</span> Imgupdate::uploadtolocal (<span style="color: #800080;">$_files</span><span style="color: #000000;">); }}</span></pre></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p></p></p><p><p>The following is a service that encapsulates an image upload.</p></p><pre><pre></pre></pre><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* Created by Phpstorm. * User:changshuiwang * DATE:2016/8/2 * time:17:51</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">namespace common\service;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> use</span><span style="color: #000000;"><span style="color: #000000;">yii;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> use</span><span style="color: #000000;"><span style="color: #000000;">sohucs\scs\scsclient;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> use</span>Yii\base\<span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> use</span><span style="color: #000000;"><span style="color: #000000;">yii\web\uploadedfile;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">imgupdate{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>scs_access_key= ' xxxxxxx '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>scs_secret_key= ' xxxxxxx '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>Host= ' xxxxxx '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #800080;"><span style="color: #800080;">$type</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Image/jpeg ', ' image/png ', ' image/jpg '<span style="color: #000000;"><span style="color: #000000;"> ); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #800080;"><span style="color: #800080;">$ext</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' JPEG ', ' png ', ' jpg '<span style="color: #000000;"><span style="color: #000000;"> ); </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @param $file * @return String returns a JSON response</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Uploadtoscs (<span style="color: #800080;"><span style="color: #800080;">$file</span></span>,<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span>= ' focus-img '<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #800080;"><span style="color: #800080;">$filename</span></span>=<span style="color: #800080;"><span style="color: #800080;">$file</span></span>[' file '] [' name '<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #800080;"><span style="color: #800080;">$tmp _name</span></span>=<span style="color: #800080;"><span style="color: #800080;">$file</span></span>[' file '] [' Tmp_name '<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Check file legitimacy</span></span>Self::checkimgext (<span style="color: #800080;"><span style="color: #800080;">$filename</span></span>,<span style="color: #800080;"><span style="color: #800080;">$tmp _name</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$file</span></span>[' file '<span style="color: #000000;"><span style="color: #000000;">]){ </span></span><span style="color: #800080;"><span style="color: #800080;">$ext</span></span>=self::getextension (<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #800080;"><span style="color: #800080;">$client</span></span>= Scsclient::factory (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Key ' = self::scs_access_key, ' secret ' + self::scs_secret_key, ' region ' => ; "bjcnc"<span style="color: #000000;"><span style="color: #000000;"> )); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if a bucket exists</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #800080;"><span style="color: #800080;">$client</span></span>->doesbucketexist (<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span><span style="color: #000000;"><span style="color: #000000;">)){ </span></span><span style="color: #800080;"><span style="color: #800080;">$result</span></span>=<span style="color: #800080;"><span style="color: #800080;">$client</span></span>->createbucket (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Buckets ' =<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span>, ' locationconstraint ' = self::<span style="color: #000000;"><span style="color: #000000;">HOST)); </span></span><span style="color: #800080;"><span style="color: #800080;">$client</span></span>->waituntilbucketexists (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span>(' Buckets ' =<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span><span style="color: #000000;"><span style="color: #000000;">)); } </span></span><span style="color: #800080;"><span style="color: #800080;">$randName</span></span>=<span style="color: #008080;"><span style="color: #008080;">uniqid</span></span>().‘.‘.<span style="color: #800080;"><span style="color: #800080;">$ext</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$filename</span></span>= "dianping/photos/".<span style="color: #008080;"><span style="color: #008080;">Date</span></span>(' Ym ',<span style="color: #008080;"><span style="color: #008080;"></span> time</span>())." /".<span style="color: #800080;"><span style="color: #800080;">$randName</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the file name after uploading</span></span> <span style="color: #800080;"><span style="color: #800080;">$pathToFile</span></span>=<span style="color: #800080;"><span style="color: #800080;">$tmp _name</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">temporary file name generated by PHP after image upload</span></span> <span style="color: #800080;"><span style="color: #800080;">$result</span></span>=<span style="color: #800080;"><span style="color: #800080;">$client</span></span>->putobject (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Buckets ' =<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span>, ' Key ' =<span style="color: #800080;"><span style="color: #800080;">$filename</span></span>, ' Body ' =<span style="color: #008080;"><span style="color: #008080;">fopen</span></span>(<span style="color: #800080;"><span style="color: #800080;">$pathToFile</span></span>, ' r+ '),<span style="color: #000000;"><span style="color: #000000;"> )); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Json_encode (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Status ' = ' success ', ' msg ' =>self::host. ' /‘.<span style="color: #800080;"><span style="color: #800080;">$bucket</span></span>.‘ /‘.<span style="color: #800080;"><span style="color: #800080;">$filename</span></span>,<span style="color: #000000;"><span style="color: #000000;"> )); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span>("picture cannot be empty"<span style="color: #000000;"><span style="color: #000000;">); } }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span> <span style="color: #800080;"><span style="color: #800080;">$e</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Json_encode (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Status ' = ' Error ', ' msg ' =<span style="color: #800080;"><span style="color: #800080;">$e</span></span>->getmessage (),<span style="color: #000000;"><span style="color: #000000;"> )); } } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @param $file uploaded picture $_files * @return string to return the JSON string</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Uploadtolocal (<span style="color: #800080;"><span style="color: #800080;">$file</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #800080;"><span style="color: #800080;">$image</span></span>=uploadedfile::usrloadfiles ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get to Picture upload class</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$image</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Check file legitimacy</span></span> <span style="color: #800080;"><span style="color: #800080;">$filename</span></span>=<span style="color: #800080;"><span style="color: #800080;">$file</span></span>[' file '] [' name '<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #800080;"><span style="color: #800080;">$tmp _name</span></span>=<span style="color: #800080;"><span style="color: #800080;">$file</span></span>[' file '] [' Tmp_name '<span style="color: #000000;"><span style="color: #000000;">]; </span>self</span>:: Checkimgext (<span style="color: #800080;"><span style="color: #800080;">$filename</span></span>,<span style="color: #800080;"><span style="color: #800080;">$tmp _name</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">move the picture to the specified directory</span></span> <span style="color: #800080;"><span style="color: #800080;">$image</span></span>=<span style="color: #800080;"><span style="color: #800080;">$image</span></span>[' file '<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #800080;"><span style="color: #800080;">$ext</span></span>=<span style="color: #800080;"><span style="color: #800080;">$image</span></span>-<span style="color: #000000;"><span style="color: #000000;">getextension (); </span></span><span style="color: #800080;"><span style="color: #800080;">$randName</span></span>=<span style="color: #008080;"><span style="color: #008080;">uniqid</span></span>() . "." .<span style="color: #800080;"><span style="color: #800080;">$ext</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$path</span></span>=<span style="color: #008080;"><span style="color: #008080;">Date</span></span>(' y-m-d ',<span style="color: #008080;"><span style="color: #008080;"></span> time</span><span style="color: #000000;"><span style="color: #000000;">()); </span></span><span style="color: #800080;"><span style="color: #800080;">$rootPath</span></span>= ' uploads/'.<span style="color: #800080;"><span style="color: #800080;">$path</span></span>. "/"<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">file upload path, using a relative path, if the/start, Linux will be from the system root directory instead of the Web root directory</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #008080;"><span style="color: #008080;">file_exists</span></span>(<span style="color: #800080;"><span style="color: #800080;">$rootPath</span></span><span style="color: #000000;"><span style="color: #000000;">)) { </span></span><span style="color: #008080;"><span style="color: #008080;">mkdir</span></span>(<span style="color: #800080;"><span style="color: #800080;">$rootPath</span></span>, 0777<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #800080;"><span style="color: #800080;">$image</span></span>->saveas (<span style="color: #800080;"><span style="color: #800080;">$rootPath</span></span>.<span style="color: #800080;"><span style="color: #800080;">$randName</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #800080;"><span style="color: #800080;">$photo</span></span>= '/'.<span style="color: #800080;"><span style="color: #800080;">$rootPath</span></span>.<span style="color: #800080;"><span style="color: #800080;">$randName</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the path to the database to which the picture is stored</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Json_encode (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Status ' = ' success ', ' msg ' =<span style="color: #800080;"><span style="color: #800080;">$photo</span></span>,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">back to foreground display</span></span><span style="color: #000000;"><span style="color: #000000;"> )); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span>("picture cannot be empty"<span style="color: #000000;"><span style="color: #000000;">); } }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span> <span style="color: #800080;"><span style="color: #800080;">$e</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Json_encode (<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">( </span></span>' Status ' = ' Error ', ' msg ' =<span style="color: #800080;"><span style="color: #800080;">$e</span></span>->getmessage (),<span style="color: #000000;"><span style="color: #000000;"> )); } } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @param $filename uploaded filename * @return mixed return file suffix</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>GetExtension (<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #800080;"><span style="color: #800080;">$info</span></span>=<span style="color: #008080;"><span style="color: #008080;">PathInfo</span></span>(<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">$info</span></span>[' extension '<span style="color: #000000;"><span style="color: #000000;">]; } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @param $filesrcname source file name * @param $filename Temporary file name * @throws Exception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Checkimgext (<span style="color: #800080;"><span style="color: #800080;">$filesrcname</span></span>,<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #800080;"><span style="color: #800080;">$ext</span></span>=self::getextension (<span style="color: #800080;"><span style="color: #800080;">$filesrcname</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine the source file type</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #008080;"><span style="color: #008080;">In_array</span></span>(<span style="color: #800080;"><span style="color: #800080;">$ext</span></span>, self::<span style="color: #800080;"><span style="color: #800080;">$ext</span></span><span style="color: #000000;"><span style="color: #000000;">)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span>("upload can only be a picture"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #800080;"><span style="color: #800080;">$size</span></span>=<span style="color: #008080;"><span style="color: #008080;">filesize</span></span>(<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$size</span></span>>=1024*1024<span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span>("upload picture is too big, not more than 1MB"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if the file name is maliciously altered</span></span> <span style="color: #800080;"><span style="color: #800080;">$magicFile</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$info</span></span>= Finfo_open (fileinfo_mime_type,<span style="color: #800080;"><span style="color: #800080;">$magicFile</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3, the picture type limit,</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$info</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #800080;"><span style="color: #800080;">$result</span></span>= Finfo_file (<span style="color: #800080;"><span style="color: #800080;">$info</span></span>,<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); Finfo_close (</span></span><span style="color: #800080;"><span style="color: #800080;">$info</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #008080;"><span style="color: #008080;">In_array</span></span>(<span style="color: #800080;"><span style="color: #800080;">$result</span></span>, self::<span style="color: #800080;"><span style="color: #800080;">$type</span></span><span style="color: #000000;"><span style="color: #000000;">)){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Exception</span></span>("illegally uploaded file Type")<span style="color: #000000;"><span style="color: #000000;">); } }} </span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p></p></p><p><p></p></p><p><p>I believe that the image upload for a web development programmer is not unfamiliar, I have done a lot of pictures before, but it seems to have never done a picture verification</p></p><p><p>private static function Checkimgext ($filesrcname, $filename) is to upload the image of some verification, php for the uploaded image, according to the configuration in php.ini, moved to $_files[' File ' [' tmp_name '],<br>$_files[' file ' [' name '] is the original name of the uploaded Image.</p></p><p><p>For some malicious programs, you may choose to modify the suffix name of the file, so that you can escape the validation of $_files, in Checkimgext () through Finfo_open can get the file mime type, so you can check the file suffix name is not malicious changes.<br>Yii inherits the rules () method for the subclass inheriting the model, and the rules () method can also judge the file type, and the underlying of Yii is verified by Finfo_open Way.</p></p><p><p>yii2.0, the single image uploaded to the Sohu ptz and the picture uploaded to the Computer.</p></p></span>