Uploading a file with twig and Slim framework (Version 2) - PHP(使用 twig 和 Slim 框架(版本 2)上传文件 - PHP)
问题描述
I'm using UserFrosting a user management system and I'm having some trouble uploading a file through form post, this is what I tried
This is how my twig file looks.
<form name="eveniment" method="post" action="{{form_action}}" enctype="multipart/form-data">
...
<input type="file" class="form-control" name="poza" id="poza">
...
</form>`
This is how my controller looks like
$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.");
}
}
Route
$app->post('/evenimente/?', function () use ($app) {
$controller = new UFEvenimentController($app);
return $controller->createEveniment();
});
PHP configuration
file_uploads On
upload_max_filesize 128M
Every other input is posted succesfully, except this one with the type="file".
I don't have any errors, I tried different ways, but with no success. Also if I print $_FILES["poza"]["name"]
it will be empty.
This answer is assuming you're using UserFrosting, since you linked this question in the UserFrosting Gitter chat.
UserFrosting includes CSRFGuard Middleware to make sure all POST requests originated locally. You need to include the CSRF token to ensure that the middleware does not block the POST request.
Since the token is already in the Twig global variables, the easiest way is to use a hidden form field with the CSRF token in it:
<input type="hidden" name="{{csrf_key}}" value="{{csrf_token}}">
这篇关于使用 twig 和 Slim 框架(版本 2)上传文件 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 twig 和 Slim 框架(版本 2)上传文件 - PHP
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01