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

PHP图片上传 - 痞子鱼

May 20, 2016 am 11:39 AM

图片上传

 

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>
로그인 후 복사

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>?>
로그인 후 복사

 

文件上传

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));

         }

 

 ?>

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)