搜索
首页php教程php手册PHP图片上传 - 痞子鱼

图片上传

 

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 Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),