首页 > 解决方案 > 使用 PHP & Javascript 获取文件输入的空白​​值以发送表单

问题描述

我面临一个问题。使用 PHP 提交表单后,我得到了文件输入的空白​​值。我在下面解释我的代码。

<form action="" method="POST" enctype="multipart/form-data">
  <div class="row form-group">
              <label  class="col-md-3 control-label text-left">Icon</label>
              <div class="col-md-8 image-upload">
                <label for="vcicon">
                  <img src="<?php if($ts->sett_img==''){ echo PT_TOURS_SLIDER_THUMB_UPLOAD.'blank.jpg'; }else{ echo $ts->sett_img; } ?>" style="cursor:pointer;width:auto; height:160px; border:1px #666 solid" id="preview_imgedit"/>
                </label>
                <input type="file" id="vciconedit" name="vciconedit" accept="image/*"/>
                <div class="input-group bmargindiv1 col-md-12">
                    <label for="vcicon" >
                      <span style="color:#00F; font-style:italic; cursor:pointer"> Click here to upload Icon </span>
                      <input type="hidden" name="someIconedit" id="someIconedit" value="<?php echo $ts->sett_img; ?>" />
                    </label>
                </div>
              </div>
            </div>
</form>

上面的部分是我的 HTML 表单,下面给出了 JavaScript 部分。

$('#vciconedit').change(function(){
      readImageData(this);
    })
    function readImageData(imgData){
      if (imgData.files && imgData.files[0]) {
        var readerObj = new FileReader();
        readerObj.onload = function (element) {
          $('#preview_imgedit').attr('src', element.target.result);
          $('#someIconedit').val('');
        } 
        readerObj.readAsDataURL(imgData.files[0]);
      }
    }


<?php 

 print_r($_FILES);exit;

?>

当我在提交后打印文件值时,给出如下值。

Array ( [vciconedit] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

在这里,我需要在提交表单后从上面的数组中获取所有值。

标签: javascriptphpfile

解决方案


推荐阅读