Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MuhammadMaaz's avatar

my iamge data is not sending thorugh post method

i am trying to save an image in data base through form using post method.but when i print the recieving data on action page,i don't get image filed,so please guide me about that issue?

0 likes
43 replies
MichalOravec's avatar

@muhammadmaaz To your form add enctype="multipart/form-data"

<form ction="/your-url" method="POST" enctype="multipart/form-data">
1 like
MuhammadMaaz's avatar

have done all necessary chores,my whole data is passing except image,just cz it's input type is file

drewdan's avatar

Can you show us your code so we can see what you are doing?

MuhammadMaaz's avatar

it's my image filed.

Image

here is my form action etc,

{{csrf_field()}}]

and here im printing my form data,

if($request->isMethod('POST')){

           $data=$request->all();

tell me if i need to show more code,

MuhammadMaaz's avatar

well im not retrieving it,im actually uploading it on database

drewdan's avatar

You need to put back ticks ``` around your code. And ideally the blade file where your form is and the controller please

MuhammadMaaz's avatar

''' if($request->isMethod('POST')){

           $data=$request->all();
           echo "<pre>";print_r($data);die;''''

here is my controller code where im printing my data sent through form

MuhammadMaaz's avatar

please guide me how can i post here my code in proper format

MuhammadMaaz's avatar
@section('content')
<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> <a href="index.html" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a> <a href="#">products</a> <a href="#" class="current">Add products</a> </div>
    <h1>products</h1>
    @if(Session::has('flash_message_error'))   
        
        <div class="alert alert-error alert-block">
	    <button type="button" class="close" data-dismiss="alert">×</button>	
        <strong>{!! Session('flash_message_error')!!}</strong>
     </div>
         @endif
         @if(Session::has('flash_message_success'))   
        
        <div class="alert alert-error alert-block">
	    <button type="button" class="close" data-dismiss="alert">×</button>	
        <strong>{!! Session('flash_message_success')!!}</strong>
     </div>
         @endif
  </div>
  <div class="container-fluid"><hr>
    <div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-info-sign"></i> </span>
            <h5>Add product</h5>
          </div>
          <div class="widget-content nopadding">
            <form enctype="multipart/form-data" class="form-horizontal" method="POST" action="{{url('/admin/add-product')}}" name="add_product" id="add_product" novalidate="novalidate">{{csrf_field()}}
            <div class="control-group">
              <label class="control-label">Under Categroy</label>
                <div class="controls">
                  
                  <select name="category_id"  id="category_id" style="width:220px;">
                  <?php echo $categories_dropdown; ?>
               
                  </select>
                </div>
              </div>
            </div>           
               <div class="control-group">
                <label class="control-label">Product Name</label>
                <div class="controls">
                  <input type="text" name="product_name" id="product_name">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label">Product Code</label>
                <div class="controls">
                  <input type="text" name="product_code" id="product_code">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label">Product Color</label>
                <div class="controls">
                  <input type="text" name="product_color" id="product_color">
                </div>
              </div>
            
              <div class="control-group">
                <label class="control-label">Description</label>
                <div class="controls">
                  <textarea name="description" id="description"></textarea>
                </div>
              </div>
              <div class="control-group">
                <label class="control-label">Price</label>
                <div class="controls">
                  <input type="text" name="price" id="price">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label">Image</label>
                <div class="controls">
                <input type="file" id="image" name="image">
                </div>
              </div>
              
              <div class="form-actions">
                <input type="submit" value="Add product" class="btn btn-success">
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
   
  </div>
</div>
@endsection```
MuhammadMaaz's avatar

namespace App\Http\Controllers;
use Auth;
use Session;
use App\Category;
use App\Product;
use Illuminate\Http\Request;

class productcontroller extends Controller
{
    public function addproduct(Request $request)
    {
        
           if($request->isMethod('POST')){
            
               $data=$request->all();
               echo "<pre>";print_r($data);die;
            //   print_r($data);die;
               if(empty($data['category_id'])){
                   return redirect()->back()->with('flash_message_error','under category is missing');
               }
               
             $product=new product;
          $product->category_id=$data['category_id'];
             $product->product_name=$data['product_name'];
              $product->product_code=$data['product_code'];
              $product->product_color=$data['product_color'];
              if(!empty($data['description'])){
                $product->description=$data['description'];
              }else{
                  $product->description='';
              }
              
              $product->price=$data['price'];
              $product->image=$data['image'];
              $product->save();
              return redirect()->back()->with('flash_message_success','product has been added successfully');

           }

        $categories= Category::where(['parent_id'=>0])->get();
        //print_r($categories);die;
    
    $categories_dropdown= "<option value='' selected disabled>select</option>";  
  //  print_r($categories_dropdown);die;
foreach($categories as $cat) {
         $categories_dropdown .="<option value='".$cat->id."'>".$cat->name."</option>";
         
         $sub_categories= Category::where(['parent_id'=>$cat->id])->get();
          //print_r($sub_categories);die;
         foreach($sub_categories as $sub_cat){
             $categories_dropdown.= "<option value='".$sub_cat->id."'>&nbsp;--&nbsp;".$sub_cat->name." </option>";
            
            
         }
        // print_r($categories_dropdown);die;
       
    }


    return view('admin.products.add-product')->with(compact('categories_dropdown'));
}


}```
MichalOravec's avatar

@muhammadmaaz Why do you use same method for get and post request?

Change this echo "<pre>";print_r($data);die; to dd($data);

And show us result.

MuhammadMaaz's avatar
  "_token" => "jla2fXLwpRuXRujQB86MFbYN6W6OqPEb64Ol12gi"
  "category_id" => "1"
  "product_name" => "dsfsd"
  "product_code" => "xewwe"
  "product_color" => "sdvd"
  "description" => "sdfsd"
  "price" => "332"
]
MuhammadMaaz's avatar

im not saving it yet,im simply checking whether image filed is being posted or not?and sadly it's not

laxminegi's avatar

You cant assign image to variable and send it with mail. You need to do several steps

Check whether image is in valid format (jpg, png...) Then upload it to sever then send it with attach and in you should use enctype="multipart/form-data"

MuhammadMaaz's avatar

i used it like that and i get empty page as an output,

  $cat= $request->file('image');
              print_r($cat);die;
		```
MichalOravec's avatar

I see that you don't have validation, do you really upload any image?

MuhammadMaaz's avatar

well i printed both suggestions and each time it truned out to be an empty page

MuhammadMaaz's avatar

then i need to send my image from form trough $_Files instead of $_post,am i correct,sorry i am yet a student,

Next

Please or to participate in this conversation.