Heim > php教程 > php手册 > PHP图片上传 - 痞子鱼

PHP图片上传 - 痞子鱼

WBOY
Freigeben: 2016-05-20 11:39:30
Original
1231 Leute haben es durchsucht

图片上传

 

Index.php文件代码:

<span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html </span><span style="color: #ff0000;">lang</span><span style="color: #0000ff;">="en"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Document<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="upload2.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">='file'</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>
Nach dem Login kopieren

upload.php代码:

图片上传步骤:

1:接收参数

         2:判断错误

         3:判断格式是否合法

         4:判断文件大小

         5:判断是是不是真正的图片

 

         6:判断是否是http post提交

<span style="color: #000000;">php 
    </span><span style="color: #0000ff;">include</span>('../function.php'<span style="color: #000000;">);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">接受参数</span>
    <span style="color: #800080;">$file</span>=<span style="color: #800080;">$_FILES</span>['file'<span style="color: #000000;">];
    </span><span style="color: #800080;">$name</span>=<span style="color: #800080;">$file</span>['name'<span style="color: #000000;">];
    </span><span style="color: #800080;">$type</span>=<span style="color: #800080;">$file</span>['type'<span style="color: #000000;">];
    </span><span style="color: #800080;">$tmp_name</span>=<span style="color: #800080;">$file</span>['tmp_name'<span style="color: #000000;">];
    </span><span style="color: #800080;">$error</span>=<span style="color: #800080;">$file</span>['error'<span style="color: #000000;">];
    </span><span style="color: #800080;">$size</span>=<span style="color: #800080;">$file</span>['size'<span style="color: #000000;">];
    </span><span style="color: #800080;">$path</span>='../images/'<span style="color: #000000;">;
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">判断错误</span>
    <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$error</span>==<span style="color: #000000;">UPLOAD_ERR_OK){
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> exit('上传成功');
        //判断格式是否合法</span>
        <span style="color: #800080;">$format</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','gif'<span style="color: #000000;">);
        </span><span style="color: #800080;">$ext</span>=format(<span style="color: #800080;">$name</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$ext</span>,<span style="color: #800080;">$format</span><span style="color: #000000;">)){
            </span><span style="color: #0000ff;">exit</span>('图片格式不正确'<span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断文件大小</span>
        <span style="color: #800080;">$allowSize</span>=1048576;            <span style="color: #008000;">//</span><span style="color: #008000;">1M</span>
        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$size</span>><span style="color: #800080;">$allowSize</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">exit</span>('图片过大'<span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是不是图片</span>
        <span style="color: #800080;">$imgSize</span>=<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$tmp_name</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$imgSize</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">exit</span>('这不是一个图片'<span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是不是通过http post上传</span>
          <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span><span style="color: #000000;">)){
       </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)){
          </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$path</span>,0777,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
          </span><span style="color: #008080;">chmod</span>(<span style="color: #800080;">$path</span>,0777<span style="color: #000000;">);
       }
       </span><span style="color: #800080;">$newName</span> = getUniqidName().".".<span style="color: #800080;">$Ext</span><span style="color: #000000;">;
       </span><span style="color: #800080;">$dstpath</span> = <span style="color: #800080;">$path</span>.'/'.<span style="color: #800080;">$newName</span><span style="color: #000000;">;
          </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>,<span style="color: #800080;">$dstpath</span><span style="color: #000000;">)){
          </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$newName</span><span style="color: #000000;">;
          }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                </span><span style="color: #0000ff;">exit</span>('图片上传失败'<span style="color: #000000;">);
          }
   }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">exit</span>('不是HTTP POST方式提交'<span style="color: #000000;">);
   }
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$error</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">case</span> '1':
                <span style="color: #0000ff;">exit</span>('文件大小超过限定值'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            
            </span><span style="color: #0000ff;">case</span> '2':
                <span style="color: #0000ff;">exit</span>('文件大小超过了表单配置大小'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> '3':
                <span style="color: #0000ff;">exit</span>('文件只有部分被上传'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> '4':
                <span style="color: #0000ff;">exit</span>('没有文件被上传'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> '6':
                <span style="color: #0000ff;">exit</span>('没有找到缓存目录'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> '7':
                <span style="color: #0000ff;">exit</span>('目录不可写'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> '8':
                <span style="color: #0000ff;">exit</span>('PHP扩展程序阻止了文件上传'<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">取文件后缀函数</span>
    <span style="color: #0000ff;">function</span> format(<span style="color: #800080;">$name</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$ext</span>=<span style="color: #008080;">explode</span>('.',<span style="color: #800080;">$name</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$val</span>=<span style="color: #008080;">end</span>(<span style="color: #800080;">$ext</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$val</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getUniqidName(){
           </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">md5</span>(<span style="color: #008080;">uniqid</span>(<span style="color: #008080;">microtime</span>(<span style="color: #0000ff;">true</span>),<span style="color: #0000ff;">true</span><span style="color: #000000;">));
    }

 </span>?>
Nach dem Login kopieren

 

文件上传

Index.php文件代码:

        

         Document

        

        

         上传">

        

 

upload.php代码:

图片上传步骤:

1:接收参数

         2:判断错误

         3:判断格式是否合法

         4:判断文件大小

         5:判断是是不是真正的图片

         6:判断是否是http post提交

代码:

         include('../function.php');

 

         //接受参数

         $file=$_FILES['file'];

         $name=$file['name'];

         $type=$file['type'];

         $tmp_name=$file['tmp_name'];

         $error=$file['error'];

         $size=$file['size'];

         $path='../images/';

        

         //判断错误

         if($error==UPLOAD_ERR_OK){

                   // exit('上传成功');

                   //判断格式是否合法

                   $format=array('jpeg','jpg','png','gif');

                   $ext=format($name);

                   if(!in_array($ext,$format)){

                            exit('图片格式不正确');

                   }

                   //判断文件大小

                   $allowSize=1048576;                          //1M

                   if($size>$allowSize){

                            exit('图片过大');

                   }

                   //判断是不是图片

                   $imgSize=getimagesize($tmp_name);

                   if(!$imgSize){

                            exit('这不是一个图片');

                   }

                   //判断是不是通过http post上传

                     if(is_uploaded_file($tmp_name)){

       if(!file_exists($path)){

          mkdir($path,0777,true);

          chmod($path,0777);

       }

       $newName = getUniqidName().".".$Ext;

       $dstpath = $path.'/'.$newName;

        if(move_uploaded_file($tmp_name,$dstpath)){

          echo $newName;

        }else{

            exit('图片上传失败');

        }

   }else{

      exit('不是HTTP POST方式提交');

   }

         }else{

                   switch ($error) {

                            case '1':

                                     exit('文件大小超过限定值');

                                     break;

                           

                            case '2':

                                     exit('文件大小超过了表单配置大小');

                                     break;

                            case '3':

                                     exit('文件只有部分被上传');

                                     break;

                            case '4':

                                     exit('没有文件被上传');

                                     break;

                            case '6':

                                     exit('没有找到缓存目录');

                                     break;

                            case '7':

                                     exit('目录不可写');

                                     break;

                            case '8':

                                     exit('PHP扩展程序阻止了文件上传');

                                     break;

                   }

         }

 

         //取文件后缀函数

         function format($name){

                   $ext=explode('.',$name);

                   $val=end($ext);

                   return $val;

         }

         function getUniqidName(){

               return md5(uniqid(microtime(true),true));

         }

 

 ?>

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage