droxy80248 2015-08-28 18:21
浏览 32
已采纳

超薄表单不传递带有图像信息的数组[关闭]

So I have an html form and it is passing my picture(the actual name), but that is all I can get. It shows an error when I try to get the tmp_name or anything of that sort.

Also, I can only get the name of the file if my html form has no declared enctype.

HTML

<form action="{{ urlFor('account.profile.post') }}" method="post" autocomplete="off">
  <!-- OTHER TEXT INPUTS THAT OUTPUT PERFECTLY FINE -->
  <div class="form-group">
    <label for="propic">Profile picture</label>
    <input type="file" id="propic" name="propic">
  </div>
</form>

PHP

if($app->request->params('propic') != null) {
  $propic = $app->request->post('propic');
  $target_dir = "profs/$img_salt/";
  $target_file = $target_dir.$propic;
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

  if (move_uploaded_file($propic["tmp_name"], $target_file)) {
    echo "The file ". basename( $propic["name"]). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
  • 写回答

1条回答 默认 最新

  • duanjie9630 2015-08-28 18:25
    关注

    You have to set the encoding type of the form to multipart/form-data:

    <form action="{{ urlFor('account.profile.post') }}" method="post" autocomplete="off" enctype="multipart/form-data">
    

    Without this encoding type, you won't be able to send files with that form.

    And in your php, grab the file from files instead of post (not sure if the following code is the way to do it in the php framework you are using):

    $propic = $app->request->files('propic');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?