5

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:

 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:

<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.

1
  • 1
    Just a note, str_random() still has a (insanely small) chance of generating a filename that does exist. Perhaps look into timestamping the file instead/as well, and also checking if that filename already exists before finalizing upload.
    – Tim Lewis
    Commented Dec 17, 2015 at 21:27

1 Answer 1

18

To send files using POST request, you need change encode of form to multipart/form-data. More details: HTML <form> enctype Attribute.

Without enctype data form send only some details about file.

2
  • This was half of the solution. I also had to change the: $file = input::get('file'); to Input::File('file'); Commented Dec 17, 2015 at 21:53
  • Another thing to keep in mind is that the "name" attribute must exist in the <input> tag, it's not enough to just have the "id" attribute, I wasted a lot of time with that issue. Commented Feb 3, 2023 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.