【问题标题】:Slim form not passing array with image information [closed]苗条的形式不传递带有图像信息的数组[关闭]
【发布时间】:2015-08-28 18:21:42
【问题描述】:

所以我有一个 html 表单,它正在传递我的图片(实际名称),但这就是我所能得到的。当我尝试获取 tmp_name 或类似的任何内容时,它会显示错误。

另外,如果我的 html 表单没有声明 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.";
  }
}

【问题讨论】:

  • 你遇到了什么错误?

标签: php html forms file-upload slim


【解决方案1】:

您必须将表单的编码类型设置为multipart/form-data

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

如果没有这种编码类型,您将无法使用该格式发送文件。

在你的 php 中,从文件而不是 post 中获取文件(不确定下面的代码是否是你正在使用的 php 框架中的方法):

$propic = $app->request->files('propic');

【讨论】:

  • 当我添加 enctype 时,我什至无法获取文件名。目前,$propic 上的 vap_dump 为我提供了文件名。当我添加 enctype 时,它​​只返回 null。
  • 那是因为你试图从 post 数组中检索文件,你必须从 files 数组 ($_FILES) 中获取它
  • 谢谢!当我在手机上时,我没有看到你回答的结尾......不过我只是使用了 $_FILES 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2017-12-25
  • 2017-08-23
  • 2012-11-28
相关资源
最近更新 更多