苗条的形式不传递带有图像信息的数组

Slim form not passing array with image information

所以我有一个 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.";
  }
}

您必须将表格的编码类型设置为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');