zoukankan      html  css  js  c++  java
  • PHP上传文件

    //HTML页面

    <form method="post" name="表单名" action="指定PHP文件或者方法"  enctype="multipart/form-data"  style="text-align:center">

    <table style="text-align:center"border="1"class="form conf_tab" cellpadding=0 cellspacing=0 >

    <tr>

     <td colspan=2 class="bottomTd">

                       <input type="file" name="flie" accept="image/jpeg,image/png,image/gif,image/bmp" />

       <input type="submit" class="button" value="提交" />

     </td>

    </tr>

    </table>

    </form>

    //PHP文件

    <?php

     $typelist=array("image/jpeg","image/jpg","image/png","image/bmp","image/gif","text/plain");//设置可上传文件类型

          $fsize = (int)$_FILES['file']['size'];

          if ($fsize > 512000)//限制上传文件最大500k

          {

             echo "<script>alert('文件超过指定的大小(500k)!');location.href='http://www.xxx.com';</script>";

             exit();

          }

        if(!in_array($_FILES['file']['type'],$typelist)){

        echo "<script>alert('上传文件格式不符合规定!');location.href='http://www.xxx.com';</script>";

        exit();

        }

    //过滤上传文件信息

    if(@$_FILES["error"]>0)

    {

        //获取错误信息

        switch($_FILES["error"]){

            case 1;

                $info="上传文件超过了php.ini最大限度值";

                break;

            case 2;

                $info="上传文件超过了html表单中限定的最大值";

                break;

            case 3;

                $info="文件只有部分上传";

                break;

            case 4;

                $info="文件没有被上传";

                break;

            case 6;

                $info="找不到临时文件";

                break;

            case 7;

                $info="文件写入失败";

                break;

        }

        die('上传文件错误,原因:'.$info);

    }

    $pathinfo = pathinfo($_FILES['file']['name']);

    $tmp=$pathinfo['extension'];

    //重命名上传文件,可以加上rand(1000,9999)

    $docName = date("YmdHis").".".$tmp;

    $uploadfile = $docName;//重定义上传文件名

    //判读上传的文件夹是否存在

    $addtime=date("Ymd",time());  

    //自定义上传文件夹的路径     

      @$testdir1="./uploadfile/";

    //自定义上传文件夹的路径  

      @$testdir2="./uploadfile/".$addtime."/"; 

      if(file_exists(@$testdir1)):   

      else:   

      mkdir(@$testdir1,0777);   

      endif; 

      if(file_exists(@$testdir2)):   

      else:   

      mkdir(@$testdir2,0777);   

      endif;  

    $path=$testdir2;

    //将上传文件信息写入数据库

    $data['url']=$PATH_NAME=$testdir2.$uploadfile;//文件上传路径

    $data['size'] =$_FILES['file']['size'];  //大小

    if(move_uploaded_file($_FILES['flie']['tmp_name'],$path.$uploadfile))

      {

        //执行sql语句

        echo "<script>alert('上传成功');location.href='http://www.xxx.com';</script>";

        exit();

      }

    else{

        echo "<script>alert('上传失败');location.href='http://www.xxx.com';</script>";

        exit();

        }

    ?>

    文章来源:微笔记_博客

  • 相关阅读:
    win32程序添加控制台程序
    STL链表类模板的学习
    简单封装的串口通信类
    nyoj49 开心的小明
    hdu2094 产生冠军
    hdu1272 小希的迷宫
    hdu1106 排序
    hdu1999 不可摸数
    hdu2085 核反应堆
    nyoj119 士兵杀敌(三)
  • 原文地址:https://www.cnblogs.com/www-vnote-net/p/5785558.html
Copyright © 2011-2022 走看看