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

    html

    <form class="formAjax"
              <?php if (!isset($datalist)): ?>action="/project/projectattachment/insertattachinfo" //初次上传图片

              <?php else: ?> action="/project/projectattachment/editattachinfo"  //修改图片

             <?php endif; ?> method="POST"
              class="formAjax form-horizontal" novalidate="novalidate"
              enctype="multipart/form-data">

                <td><p class="short-input ue-clear">
                            <label>附件名称:</label>
                            <input name="annexname" type="text" id="annexname"/>
                            <input name="id" type="hidden" id="id"/>
                            <a name="id" id="sky_txt"/>
                        </p>

                 </td> 

    ajax

    $('.formAjax').ajaxSubmit(
                    {
                        type:'POST',
                        url:'/project/projectattachment/insertattachinfo',
                        beforeSumbit: function () {
                            $("#sky_txt").html("图片上传中...");
                        },
                        success:function(data)
                        {
                            var dataJSON = $.parseJSON(data)
                            if(dataJSON.success == '0')
                            {
                                alert(dataJSON.msg);
                            }
                            {
                                window.parent.getDataList(0,10);
                            }
                        }
                    }
                )

    php代码

     public function insertattachinfo()
        {
            $name=$_FILES['file']['name'];
            $newname=explode('.',$name);  //explode() 函数把字符串打散为数组。explode(separator,string,limit);separator:必需。规定在哪里分割字符串。string:必需。要分割的字符串。
            $namesize=count($newname);
            $size = $_FILES['file']['size'];
            $insertAccidentFilesData = array();
            $title=explode('.'.$newname[$namesize-1],$name)[0];
            if(!empty($_FILES))
            {
                $path = '/uploads/filerurl/'.$_POST['projectcode']."/";
                $targetFolder=  FCPATH.$path;
                if (!is_dir($targetFolder)) mkdir($targetFolder); //is_dir判断目录是否存在
                $strarr = explode('.',$_FILES['file']['name']);
                $filename = str_replace(" ","", $strarr[0].time().'.'.$strarr[1]); //str_replace() 函数使用一个字符串替换字符串中的另一些字符。
                $futurename=iconv("utf-8","gb2312",rtrim($targetFolder,'/') . '/' .$filename); //iconv函数库能够完成各种字符集间的转换
                if(is_uploaded_file($_FILES['file']['tmp_name'])  && move_uploaded_file($_FILES['file']['tmp_name'],$futurename)){
                        $annextype = '其他';
                    if(strstr($_FILES['file']['type'],'image'))
                        $annextype = '图片';
                     
                    $insertAccidentFilesData = array(
                        'type' => "项目附件",
                        'annexname'   => $_POST['annexname'],
                        'annexurl'   => $path.$filename,
                        'projectcode'  => $_POST['projectcode'],
                        'annextype' => $annextype,
                        'createuser'  => $this->sessioninfo['fullName'],
                        'createdatetime' => date('Y-m-d H:i:s'),
                        
                    );
                    $insertFile = $this->db->insert('sysattachs', $insertAccidentFilesData);
                    $this->setLogs("上传附件",$insertAccidentFilesData);
                    $result = array(
                        'success' => '1',
                        'msg' => '上传文件成功!',
                    );
            
                }else{
                    $result = array(
                        'success' => '0',
                        'msg' => '上传文件失败!',
                    );
                    echo "<script>alert('上传文件失败!');</script>";
                }
                echo json_encode($result);
                //echo "<script>parent.location.reload();</script>";
                    
            }
        }

    public function editattachinfo() {
            $id = $_POST['id'];
            $annexname = $_POST['annexname'];
            $sql = "select * from  sysattachs where id = '".$id."'"; //数据库SQL语句查询
            $query = $this->mydb->find($sql); //执行SQL语句
            $this->setLogs("修改附件信息前",$query['obj']);
            $sql = "update sysattachs set annexname = '".$annexname."' where id = ".$id.""; //更新数据库
            $this->mydb->execSql($sql);
            $sql = "select * from  sysattachs where id = '".$id."'";
            $query = $this->mydb->find($sql);
            $this->setLogs("修改附件信息后",$query['obj']);
            echo json_encode($query);
        }

  • 相关阅读:
    Lucas定理及其应用
    HDU 5044 TREE
    HDU 5033 Building
    Codeforces Round #238 (Div. 1)
    hdu 4878 ZCC loves words AC自动机+中国剩余定理+快速幂
    HDU 5015 233 Matrix
    HDU 5008 Boring String Problem
    ZOJ 3817 Chinese Knot
    使用AutoMapper
    多租户概念以及FreeLink多租户设计思想
  • 原文地址:https://www.cnblogs.com/lqw4/p/4662053.html
Copyright © 2011-2022 走看看