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

原文地址:https://www.cnblogs.com/lqw4/p/4662053.html