用twig和Slim框架(版本2)上传文件 - PHP [英] Uploading a file with twig and Slim framework (Version 2) - PHP

查看:457
本文介绍了用twig和Slim框架(版本2)上传文件 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UserFrosting一个用户管理系统,而且我在通过表单发布文件时遇到了一些麻烦,这就是我试过的一样。
$ b

 < form name =evenimentmethod =postaction = {{form_action}}enctype =multipart / form-data> 
...
< input type =fileclass =form-controlname =pozaid =poza>
...
< / form>`

是我的控制器的样子

  $ target_dir =uploads /; 
$ target_file = $ target_dir。基名($ _ FILES [ 波萨] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);

//检查图像文件是实际图像还是假图像
$ check = getimagesize($ _ FILES);
if($ check!== false){
$ ms-> addMessage(success,File is a image - 。$ check [mime]。。);
$ uploadOk = 1;
} else {
$ ms-> addMessage(危险,文件不是图像。
$ uploadOk = 0;
}
$ ms-> addMessage(success,$ target_file);
//检查文件是否已经存在
if(file_exists($ target_file)){
$ ms-> addMessage(danger,Sorry,file already exists。
$ uploadOk = 0;

//检查文件大小
if($ _FILES [poza] [size]> 500000){
$ ms-> addMessage( ,对不起,你的文件太大了);
$ uploadOk = 0;

//如果$ uploadOk == 0 {
$ ms-> addMessage(danger,
),检查$ uploadOk是否被设置为0 对不起,你的文件没有上传。);
//如果一切正常,尝试上传文件
} else {
if(move_uploaded_file($ _ FILES [poza] [name],$ target_file)){
$ ms-> addMessage(success,The file。basename($ _FILES [poza] [name])。已上传。
} else {
$ ms-> addMessage(危险,对不起,上传文件时出现错误。




路由

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

PHP配置



upload_max_filesize 128M



其他所有输入都会成功发布,除了这个输入是type =file。

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

解决方案

这个答案假设你使用UserFrosting,因为你在UserFrosting Gitter聊天中链接了这个问题。
$ b UserFrosting包括CSRFGuard中间件确保所有的POST请求在本地发起。您需要包含CSRF令牌以确保中间件不会阻止POST请求。

由于令牌已经在Twig全局变量中,所以最简单的方法是使用一个带有CSRF标记的隐藏表单域:

 < input type =hiddenname ={{csrf_key }}value ={{csrf_token}}> 


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 UF\EvenimentController($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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆