0

Laravel 4 Input file return empty after post .. why?

html code in blade file

<div id='images'>
<input type='hidden' id='imagescounter' name='imagescounter' value='1'>
<input type='file' id='image_1' name='image_1' class='col-md-12' onchange='$("#task").val("uploadtotemp");$("#temp_count").val(1);$("#addbirdfrm").submit();' >
</div>
<?php
    $image = asset("images/controls/previewbird.png");
?>
<a href='#' onclick='addimageinput($("#imagescounter").val(),"{{ $image }}");return false;' id='addimageinput'><span class="glyphicon glyphicon-plus-sign top-1" aria-hidden="true"></span> </a>
<div id='previews'>
<input type='hidden' name='temp_count' id='temp_count' value=''>
<img id='preview_1' name='preview_1' src='{{ asset("images/controls/previewbird.png") }}'>
</div>

and jquery in the same file

$("#addbirdfrm").submit(function(e){
         if($('#task').val() == 'uploadtotemp'){
            $('#preview_'+$('#temp_count').val()).attr('src','{{ asset("images/controls/loading_facebook.gif") }}');
            var dataString = $('#addbirdfrm').serializeArray();
            e.preventDefault();
            var token =  $("input[name=_token]").val();
            $.ajax({
                type: "POST",
                url : "{{ URL::route('get-temp') }}",
                data : dataString,
                success : function(data){
                    //$('#date').val(data);
                    alert(data);
                }
            },"json");
            return false;
        }
    });

and in controller file

public function postGetTemp(){
        return Input::file('image_1')->getClientOriginalName();
}

Why this code return empty data?

and data return 0 when change controller code to

public function postGetTemp(){
        return 0;
}

so why Input::file('image_1') didn't return data? thanks

1 Answers1

0

Input::file() is a Symfony\Component\HttpFoundation\File\UploadedFile object, not a printable element. Use var_dump() or dd() to read it.

phaberest
  • 3,140
  • 3
  • 32
  • 40
  • returned array(14) { ["id"]=> string(2) "12" ["gander"]=> string(4) "male" ["category_0"]=> string(1) "0" ["category_counter"]=> string(1) "0" ["full_category"]=> string(0) "" ["date"]=> string(10) "2014-11-28" ["age"]=> string(0) "" ["ringdetails"]=> string(0) "" ["nots"]=> string(0) "" ["imagescounter"]=> string(1) "1" ["temp_count"]=> string(1) "1" ["task"]=> string(12) "uploadtotemp" ["where"]=> string(4) "bird" ["_token"]=> string(40) "LCx75UalaBqEsTsKMi3HbdofveNP3JlW8uSnkiJr"} not returned image_1 :( – khaledbelal Nov 28 '14 at 18:03
  • What do you need to do with this file? You have to use `Input::file('image_1')->move($destinationPath, $fileName);` to upload it. – phaberest Nov 28 '14 at 18:06
  • Input::file('image_1')->getClientOriginalName(); return null why? – khaledbelal Nov 28 '14 at 18:08
  • Take an eye on http://laravel.com/docs/4.2/requests#files to understand the methods you can use on `Input::file()` (there are a few) – phaberest Nov 28 '14 at 18:09
  • can't get $fileName from Input::file('image_1') because return null – khaledbelal Nov 28 '14 at 18:10
  • Try to check if it gets it with `Input::hasFile('image_1')` – phaberest Nov 28 '14 at 18:10
  • returned false bool(false) – khaledbelal Nov 28 '14 at 18:16
  • I found out what's your problem. You can't upload a file using `serializeArray()`. Take a look on http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery/8758614#8758614 – phaberest Nov 28 '14 at 18:17
  • when i used var_dump(Input::all()) returned all input true values but only image_1 input not return value – khaledbelal Nov 28 '14 at 18:18
  • 1
    Hey @khaledbelal is it possible that maybe you forgot to set `enctype="multipart/form-data"` on the `form`? – phaberest Nov 29 '14 at 22:26