文件上传不起作用 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34343575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File upload does not work Laravel
提问by Sander Van Keer
I'm trying to upload an image from a form and store it in my laravelproject. The folder I want to store these pictures in is calles battlePics and I made this folder on the assets level. The actual problem is that $filename remains empty after this function. This is my controller:
我正在尝试从表单上传图像并将其存储在我的 laravelproject 中。我想存储这些图片的文件夹叫做 BattlePics,我在资产级别创建了这个文件夹。实际的问题是 $filename 在这个函数之后仍然是空的。这是我的控制器:
if (Input::hasFile('file')) {
$file = input::get('file');
$string = str_random(40);
$filename = hash("md5", $string) . $file->getClientOriginalName();
$file->move('battlePics', $filename);
$battle->picture = $filename;
}
$battle->save();
I use the md5 hash to create different filenames to avoid errors when an image with the same name is uploaded. Here is the view of my form:
我使用 md5 哈希创建不同的文件名,以避免在上传具有相同名称的图像时出错。这是我的表单的视图:
<form id="battleForm" class="battleForm" action="" method="post" role="form">
<input type="hidden" name="userID" value="{{Auth::id()}}">
<div class="form-group">
<label for="title">Title of the battle?</label><br>
<input type="text" id="title" name="title" placeholder=""><br>
</div>
<div class="form-group">
<label for="points">Points to be earned</label><br>
<input type="text" id="points" name="points" placeholder=""><br>
</div>
<div class="form-group date">
<label for="startdate">Startdate</label><br>
<input type="date" id="startdate" name="startdate" placeholder=""><br>
</div>
<div class="form-group date">
<label for="enddate">Enddate</label><br>
<input type="date" id="enddate" name="enddate" placeholder=""><br>
</div>
<div class="form-group">
<label for="description">What is the battle about?</label><br>
<textarea type="text" id="description" name="description" placeholder=""></textarea><br>
</div>
<div class="form-group radiobuttons">
<label for="group1">Want to start de battle right away?</label><br>
<ul>
<li><input class="radiobattle" type="radio" name="active" value="yes"> <span>yes</span></li>
<li><input class="radiobattle" type="radio" name="active" value="no"><span>no</span><br></li>
</ul>
</div>
<div class="form-group battleimg uploadbatlle field">
<img id="previewNewFile" src="" style="height:100px; width:150px;display: none; " required="true"/>
<input style="" type="file" name="file" id="file" required="true">
</div>
<br>
<input type="submit" name="battleSubmit" class="battleSubmit" value="Add new Battle"><br>
<input type="hidden" class="tokenReferEmail" name="_token" value="<?php echo csrf_token(); ?>">
</form>
I really hope you guys can help me.
我真的希望你们能帮助我。
回答by Grzegorz Gajda
To send files using POST
request, you need change encode of form to multipart/form-data
. More details: HTML <form> enctype Attribute.
要使用POST
请求发送文件,您需要将表单编码更改为multipart/form-data
. 更多细节:HTML <form> enctype 属性。
Without enctype
data form send only some details about file.
没有enctype
数据形式只发送有关文件的一些详细信息。