Php 文件上载不起作用Laravel

Php 文件上载不起作用Laravel,php,image,laravel,upload,controller,Php,Image,Laravel,Upload,Controller,我正在尝试从表单上传一个图像,并将其存储在我的laravelproject中。我想存储这些图片的文件夹叫做battlePics,我在资产级别创建了这个文件夹。实际问题是$filename在此函数之后仍然为空。这是我的控制器: if (Input::hasFile('file')) { $file = input::get('file'); $string = str_random(40); $filename = hash("md5", $string

我正在尝试从表单上传一个图像,并将其存储在我的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();
我使用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>

战斗名称?

要获得的积分

起始日期

结束日期

这场战斗是关于什么的?

想马上开始战斗吗?




要使用
POST
请求发送文件,需要将表单编码更改为
multipart/form data
。更多详细信息:


如果没有
enctype
数据表单,则只发送有关文件的一些详细信息。

只需注意,
str_random()
仍然有机会生成确实存在的文件名。也许还可以查看文件的时间戳,并在完成上传之前检查该文件名是否已经存在。这是解决方案的一半。我还必须更改:$file=input::get('file');输入::File('File');