首頁 >後端開發 >php教程 >javascript - 怎么把html上文件选择器选中的文件上传到服务器指定目录并把patch记录到数据库呢?

javascript - 怎么把html上文件选择器选中的文件上传到服务器指定目录并把patch记录到数据库呢?

WBOY
WBOY原創
2016-06-06 20:32:582030瀏覽

html部分:

<code><extend name="Common/base"></extend>
<block name="nav"></block>

<block name="main">
<div class="pd-20">
   <form action="addPost" method="post" class="form form-horizontal" id="form-staff-add">
    <input type="hidden" id="{$modelIdField}" name="{$modelIdField}" value="{$staff.staff_id}">
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>图片文件:</label>
      <div class="formControls col-7">
        <span class="btn-upload form-group">
            <input class="input-text upload-url radius" type="text" name="uploadfile" id="uploadfile" readonly datatype="*" nullmsg="不能为空!"><a href="javascript:void();" class="btn btn-primary radius"><i class="iconfont">󰀠</i> 浏览文件</a>
            <input type="file" multiple name="file" class="input-file">
        </span>
      </div>
      <div class="col-4"> </div>
    </div>
    <div class="row cl">
      <div class="col-9 col-offset-3">
        <input class="btn btn-primary radius" type="submit" value="  提交  ">
      </div>
    </div>
  </form>
</div>
</block>

<block name="js">
$(function(){
    $("#form-staff-add").Validform({
        tiptype:3,
    });
});
</block>
</code>

php部分:

<code><?php /**
 * PHP多文件上传模块
 *
 * 用到的函数:is_upload_file(), move_upload_file(), pathinfo(), file_exists()
 */


//1.获取要上传文件的信息
$up_info = @$_FILES['myfile'];
$to_path = "./pic";
$typelist = array("image/gif","image/jpeg", "image/pjpeg", "image/png");

//在循环里面验证上传相关信息
for ($i = 0; $i < count($up_info['name']); $i++) {

    //2.判断文件是否上传错误
    if ($up_info['error'][$i]>0){
        switch ($up_info['error'][$i]){
            case 1:
                $err_info="上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                $err_info="上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
                break;
            case 3:
                $err_info="文件只有部分被上传";
                break;
            case 4:
                $err_info="没有文件被上传";
                break;
            case 6:
                $err_info="找不到临时文件夹";
                break;
            case 7:
                $err_info="文件写入失败";
                break;
            default:
                $err_info="未知的上传错误";
                break;
        }
        continue($err_info);
    }

    //3.判断文件上传的类型是否合法
    if (!in_array($up_info['type'][$i], $typelist)){
        continue("文件类型错误!".$up_info['type'][$i]);
    }

    //4.上传文件的大小过滤
    if ($up_info['size'][$i]>1000000){
        continue("文件大小超过1000000");
    }


    //5.上传文件名处理
    $exten_name = pathinfo($up_info['name'][$i], PATHINFO_EXTENSION);
    do{
        $main_name=date("YmdHis")."--".rand(100, 999);
        $new_name = $main_name.".".$exten_name;
    }while (file_exists($to_path.'/public/upload/photo'.$new_name));



    //6.判断是否是上传的文件,并执行上传
    if (is_uploaded_file($up_info['tmp_name'][$i])){
        if (move_uploaded_file($up_info['tmp_name'][$i], $to_path.'/public/upload/photo'.$new_name)){
            echo "文件上传成功!";
        }else{
            echo "文件移动失败!";
        }
    }

}
?>

貌似是不是需要先让html能调用到php脚本啊
</code>

回复内容:

html部分:

<code><extend name="Common/base"></extend>
<block name="nav"></block>

<block name="main">
<div class="pd-20">
   <form action="addPost" method="post" class="form form-horizontal" id="form-staff-add">
    <input type="hidden" id="{$modelIdField}" name="{$modelIdField}" value="{$staff.staff_id}">
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>图片文件:</label>
      <div class="formControls col-7">
        <span class="btn-upload form-group">
            <input class="input-text upload-url radius" type="text" name="uploadfile" id="uploadfile" readonly datatype="*" nullmsg="不能为空!"><a href="javascript:void();" class="btn btn-primary radius"><i class="iconfont">󰀠</i> 浏览文件</a>
            <input type="file" multiple name="file" class="input-file">
        </span>
      </div>
      <div class="col-4"> </div>
    </div>
    <div class="row cl">
      <div class="col-9 col-offset-3">
        <input class="btn btn-primary radius" type="submit" value="  提交  ">
      </div>
    </div>
  </form>
</div>
</block>

<block name="js">
$(function(){
    $("#form-staff-add").Validform({
        tiptype:3,
    });
});
</block>
</code>

php部分:

<code><?php /**
 * PHP多文件上传模块
 *
 * 用到的函数:is_upload_file(), move_upload_file(), pathinfo(), file_exists()
 */


//1.获取要上传文件的信息
$up_info = @$_FILES['myfile'];
$to_path = "./pic";
$typelist = array("image/gif","image/jpeg", "image/pjpeg", "image/png");

//在循环里面验证上传相关信息
for ($i = 0; $i < count($up_info['name']); $i++) {

    //2.判断文件是否上传错误
    if ($up_info['error'][$i]>0){
        switch ($up_info['error'][$i]){
            case 1:
                $err_info="上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                $err_info="上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
                break;
            case 3:
                $err_info="文件只有部分被上传";
                break;
            case 4:
                $err_info="没有文件被上传";
                break;
            case 6:
                $err_info="找不到临时文件夹";
                break;
            case 7:
                $err_info="文件写入失败";
                break;
            default:
                $err_info="未知的上传错误";
                break;
        }
        continue($err_info);
    }

    //3.判断文件上传的类型是否合法
    if (!in_array($up_info['type'][$i], $typelist)){
        continue("文件类型错误!".$up_info['type'][$i]);
    }

    //4.上传文件的大小过滤
    if ($up_info['size'][$i]>1000000){
        continue("文件大小超过1000000");
    }


    //5.上传文件名处理
    $exten_name = pathinfo($up_info['name'][$i], PATHINFO_EXTENSION);
    do{
        $main_name=date("YmdHis")."--".rand(100, 999);
        $new_name = $main_name.".".$exten_name;
    }while (file_exists($to_path.'/public/upload/photo'.$new_name));



    //6.判断是否是上传的文件,并执行上传
    if (is_uploaded_file($up_info['tmp_name'][$i])){
        if (move_uploaded_file($up_info['tmp_name'][$i], $to_path.'/public/upload/photo'.$new_name)){
            echo "文件上传成功!";
        }else{
            echo "文件移动失败!";
        }
    }

}
?>

貌似是不是需要先让html能调用到php脚本啊
</code>

我php不会所以不清楚你后台的代码是否正确,但是你的from的类型就不对:

<code>enctype="multipart/form-data"
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn