zoukankan      html  css  js  c++  java
  • PHP——图片上传

    图片上传

     

    Index.php文件代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="upload2.php" method="post" enctype="multipart/form-data">
        <input type="file" name='file'>
        <input type="submit" value="上传">
        </form>
    </body>
    </html>

    upload.php代码:

    图片上传步骤:

    1:接收参数

             2:判断错误

             3:判断格式是否合法

             4:判断文件大小

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

             6:判断是否是http post提交

    <?php 
        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));
        }
    
     ?>

    文件上传

    Index.php文件代码:

    <!DOCTYPE html>

    <html lang="en">

    <head>

             <meta charset="UTF-8">

             <title>Document</title>

    </head>

    <body>

             <form action="upload2.php" method="post" enctype="multipart/form-data">

             <input type="file" name='file'>

             <input type="submit" value="上传">

             </form>

    </body>

    </html>

     

    upload.php代码:

    图片上传步骤:

    1:接收参数

             2:判断错误

             3:判断格式是否合法

             4:判断文件大小

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

             6:判断是否是http post提交

    代码:

    <?php

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

             }

     

     ?>

    痞子鱼->我曾七次鄙视自己的灵魂, 第一次,当它本可进取时,却故作谦卑; 第二次,当它在空虚时,用爱欲来填充; 第三次,在困难和容易之间,它选择了容易; 第四次,它犯了错,却借由别人也会犯错来宽慰自己; 第五次,它自由软弱,却把它认为是生命的坚韧; 第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副; 第七次,它侧身于生活的污泥中,虽不甘心,却又畏首畏尾。
  • 相关阅读:
    eclipse里maven项目An error occurred while filtering resources解决办法
    python中分页使用
    es学习
    cmdb资产管理2
    saltstack安装使用
    Django Rest Framework
    免交互批量分发公钥的实现
    单链表复制早已难不到你,但若我们再加个指针...
    面试 16:栈的压入压出队列(剑指 Offer 第 22 题)
    面试 15:顺时针从外往里打印数字(剑指 Offer 第 20 题)
  • 原文地址:https://www.cnblogs.com/piziyu/p/5428828.html
Copyright © 2011-2022 走看看