7

我正在使用 UserFrosting 用户管理系统,我在通过表单发布上传文件时遇到了一些问题,这就是我尝试过的

这就是我的树枝文件的外观。

<form name="eveniment" method="post" action="{{form_action}}" enctype="multipart/form-data">
  ...
  <input type="file" class="form-control" name="poza" id="poza">
  ...
</form>`

这就是我的控制器的样子

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["poza"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
$check = getimagesize($_FILES);
if($check !== false) {
    $ms->addMessage("success", "File is an image - " . $check["mime"] . ".");
    $uploadOk = 1;
} else {
    $ms->addMessage("danger", "File is not an image.");
    $uploadOk = 0;
}
$ms->addMessage("success", $target_file);
// Check if file already exists
if (file_exists($target_file)) {
    $ms->addMessage("danger", "Sorry, file already exists.");
    $uploadOk = 0;
}
// Check file size
if ($_FILES["poza"]["size"] > 500000) {
    $ms->addMessage("danger", "Sorry, your file is too large.");
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    $ms->addMessage("danger", "Sorry, your file was not uploaded.");
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["poza"]["name"], $target_file)) {
        $ms->addMessage("success", "The file ". basename( $_FILES["poza"]["name"]). " has been uploaded.");
    } else {
        $ms->addMessage("danger",  "Sorry, there was an error uploading your file.");
    }
}

路线

$app->post('/evenimente/?', function () use ($app) {
    $controller = new UF\EvenimentController($app);
    return $controller->createEveniment();
});

PHP 配置

file_uploads On

upload_max_filesize 128M

其他所有输入都已成功发布,除了 type="file" 的输入。

我没有任何错误,我尝试了不同的方法,但没有成功。此外,如果我打印$_FILES["poza"]["name"]它将是空的。

4

1 回答 1

3

此答案假设您使用的是 UserFrosting,因为您在 UserFrosting Gitter 聊天中链接了此问题。

UserFrosting 包含 CSRFGuard 中间件,以确保所有 POST 请求都源自本地。您需要包含 CSRF 令牌以确保中间件不会阻止 POST 请求。

由于令牌已经在 Twig 全局变量中,最简单的方法是使用带有 CSRF 令牌的隐藏表单字段:

<input type="hidden" name="{{csrf_key}}" value="{{csrf_token}}">
于 2016-02-24T16:37:57.307 回答