文件上传不起作用Laravel

时间:2015-12-17 20:22:03

标签: php image laravel upload controller

我尝试从表单上传图片并将其存储在我的laravelproject中。我要存储这些图片的文件夹是calles 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>

我真的希望你们能帮助我。

1 个答案:

答案 0 :(得分:5)

要使用POST请求发送文件,您需要将表单格式更改为multipart/form-data。更多详情:HTML <form> enctype Attribute

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