图片代码上传问题-laravel

时间:2018-11-21 18:28:35

标签: php jquery html arrays laravel

希望您一切都好。我不熟悉laravel。我添加产品的代码有问题。 问题恰好在product_image中。我没有图像,并且不断收到消息“产品添加成功!没有图像”

public function save_product(Request $request)
{
    $data=array();
    $data['product_name']=$request->product_name;
    $data['category_id']=$request->category_id;
    $data['manufacture_id']=$request->manufacture_id;
    $data['product_short_description']=$request->product_short_description;
    $data['product_long_description']=$request->product_long_description;
    $data['product_price']=$request->product_price;
    $data['product_size']=$request->product_size;
    $data['product_image']=$request->product_image;
    $data['product_color']=$request->product_color;
    $data['publication_status']=$request->publication_status;
    $image=$request->file('product_image');
    if ($image) {
        $image_name=str_random(20);
            $ext=strtolower($image->getClientOriginalExtension());
            $image_full_name=$image_name.'.'.$ext;
            $upload_path='image/';
            $image_url=$upload_path.$image_full_name;
            $success=$image->move($upload_path,$image_full_name);
            if ($success) {
                $data['product_image']=$image_url;
                DB::table('tbl_products')->insert($data);
                Session::put('message','Product added Successfully!');
                return Redirect::to('/add-product');
            }
        //echo "<pre>";
        //print_r($data);
        //echo "</pre>";
        //exit();
    }
    $data['product_image']='';
    DB::table('tbl_products')->insert($data);
    Session::put('message','Product added Successfully! without image');
    return Redirect::to('/add-product');
}

试图解决问题。我在if语句中添加了以下代码,以查看$ data中的内容:

echo "<pre>";
        print_r($data);
        echo "</pre>";
        exit();

但是我什么都没有。只是空白页 然后我在最后4行之前添加了相同的代码。我收到了数组中的内容,并且data [“ product_image”]为空。

这是表格(我没有忘记输入enctype):

<form class="form-horizontal" action="{{ url('/save-product')}}" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <fieldset>
        <div class="control-group">
            <label class="control-label" for="date01">Product Name</label>
            <div class="controls">
                <input type="text" class="input-xlarge" name="product_name" required="">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="selectError3">Category name</label>
                <div class="controls">
                    <select id="selectError3" name="category_id">
                        <option>Select Category</option>
                                        <?php
                                                $all_published_category=DB::table('tbl_category')
                                                                                                ->where('publication_status',1)
                                                                                                ->get();
                                                foreach($all_published_category as $v_category) {?>
                        <option>{{$v_category->category_name}}</option>
                                        <?php } ?>
                    </select>
                </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="selectError3">Brand name</label>
            <div class="controls">
                <select id="selectError3" name="manufacture_id">
                    <option>Select Brand</option>
                                        <?php
                                                $all_published_manufacture=DB::table('tbl_manufacture')
                                                                                                ->where('publication_status',1)
                                                                                                ->get();
                                                foreach($all_published_manufacture as $v_manufacture) {?>
                    <option>{{$v_manufacture->manufacture_name}}</option>
                                        <?php } ?>
                </select>
            </div>
        </div>                        
        <div class="control-group hidden-phone">
            <label class="control-label" for="textarea2">Product Short Description</label>
            <div class="controls">
                <textarea class="cleditor" name="product_short_description" rows="3" required=""></textarea>
            </div>
            <div class="control-group hidden-phone">
                <label class="control-label" for="textarea2">Product Long Description</label>
                <div class="controls">
                    <textarea class="cleditor" name="product_long_description" rows="3" required=""></textarea>
                </div>
                <div class="control-group">
                    <label class="control-label" for="date01">Product Price</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge" name="product_price" required="">
                        </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="fileInput">Image</label>
                    <div class="controls">
                        <input class="input-file uniform_on" name="product_image" id="product_image" type="file">
                    </div>
                </div>   
                <div class="control-group">
                    <label class="control-label" for="date01">Product Size</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="product_size" required="">
                    </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="date01">Product Color</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="product_color" required="">
                </div>
            </div>
            <div class="control-group hidden-phone">
                <label class="control-label" for="textarea2">Publication status </label>
                <div class="controls">
                    <input type="checkbox" name="publication_status" value="1">
                </div>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Add product</button>
                <button type="reset" class="btn">Cancel</button>
            </div>
    </fieldset>
</form>   

先谢谢您!

2 个答案:

答案 0 :(得分:0)

检查您的代码

<input class="input-file uniform_on" id="product_image" type="file">

没有名称属性。添加它并解决问题。

<input class="input-file uniform_on" name="product_image" id="product_image" type="file">

答案 1 :(得分:0)

 // Get Form Image
         $image = $request->file('image');
        $slug = str_slug($request->name);
        if (isset($image))
        {
            $currentDate = Carbon::now()->toDateString();
            $imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
            if (!file_exists('storage/uploads/post'))
            {
                mkdir('storage/uploads/post',0777,true);
            }
            $image->move('storage/uploads/post',$imagename);
        }else{
            $imagename = "default.png";
        }