无法通过表单上传图片?

时间:2018-05-29 08:10:45

标签: php html laravel-5

这是我的HTML:

                            <label for="attachement1">Attach a file: <small style="color:#999;">(type: zip/rar and below 10mb)</small></label>

                            <input type="file" name="file1"/><br/>
                             <label for="snapshot">Snapshot / Thumbnail:</label>

                            <input type="file" name="thumbnail" required/><br/>
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">

                            <input type="submit" class="btn btn-primary" name="Submit" value="Publish" />

以下是我的控制器文件中的代码(用于更新功能):

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
      $this->validate($request, [

      'thumbnail' => 'mimes:jpg,jpeg,png|max:800',

      'file1' => 'mimes:rar,zip|max:10000',

      ]);



      $file1=$request->file('file1');

      if(is_null($request->file('file1'))){

        $p=pages::where('id', '=', $request['id'])->first();

        $attmt1=$p->attachment;

      }

      else

      {

      $upload_dir='uploads';

    $attmt1=$file1->getClientOriginalName();

    $move=$file1->move($upload_dir, $attmt1);

      }



      if(is_null($request->file('thumbnail'))){

        $p=pages::where('id', '=', $request['id'])->first();

        $image=$p->thumbnail;

      }

      else

      {

        $img=$request->file('thumbnail');

        $upload_dir='thumbnails';

        $image=$img->getClientOriginalName();

        $move=$img->move($upload_dir, $image);

        //end thumbnail process 

      }

    $mypage->title = $request->title;
    $mypage->body = $request->body;
    //$mypage->thumbnail = $request->thumbnail;
    $mypage->slug = str_slug($request->slug, '-');
    $mypage->menu_name = $request->menu_name;
    $mypage->save();

    return redirect()->route('menupages.index')->with('message', 'Page updated successfully.');
}

当我尝试编辑项目并上传图像(.jpg格式),然后点击提交时,我得到一个&#34;缩略图必须是以下类型的文件:jpg,jpeg,png。&#34;我检查了数据库,但没有记录文件。

出于某种原因,它将图像检测为一些外来图像文件类型,即使它是.jpg。

3 个答案:

答案 0 :(得分:0)

您是否添加了enctype =&#34; multipart / form-data&#34;在你的表格上?

 <form method="post" Action= "" enctype="multipart/form-data">

 </form

答案 1 :(得分:0)

如果您想上传某些内容,则需要将以下代码添加到表单中。

enctype="multipart/form-data"

如果您不这样做,则无法上传内容。 你有没有把它添加到你的html表单?

答案 2 :(得分:0)

我得到了开发人员的帮助,所以我将发布我们如何解决问题的方法。

以下是该功能的完整修订代码:

控制器:

  public function update(Request $request, $id)
 {
       $this->validate($request, [

      'thumbnail' => 'mimes:jpg,jpeg,png|max:300000',

      'file1' => 'mimes:rar,zip|max:10000',

      ]);



      $file1 = $request->file('file1');
      if(is_null($request->file('file1'))){

      //  $p=pages::where('id', '=', $request['id'])->first();
        $p = MenuPage::find($request['id']);
        $attmt1 = $p['attachment'];

      }

      else

      {

      $upload_dir = 'uploads';

    $attmt1 = $file1->getClientOriginalName();

    $file1->move($upload_dir, $attmt1);

      }



      if(is_null($request->file('thumbnail'))){

      //  $p=pages::where('id', '=', $request['id'])->first();
        $p = MenuPage::findOrFail($request['id']);

        $image = $p->thumbnail;

      }

      else

      {

        $img = $request->file('thumbnail');

        $upload_dir = 'thumbnails';

        $image = $img->getClientOriginalName();

        $img->move($upload_dir, $image);

        //end thumbnail process 

      }

    //$check=pages::where('id', $request['id'])

    //->update([

    //  'title' => $title,

    //  'body' =>$body, 

    //  'thumbnail' =>$thumbnail, 

   //   'slug' =>$slug, 

    //  'school' =>$school, 

   //   'attachment' =>$attmt1, 

   //   'menu_name' =>$menu_name,

   //   ]);

    $mypage = MenuPage::find($id);
    $mypage->title = $request->title;
    $mypage->body = $request->body;
   $mypage->thumbnail = $image;
    $mypage->attachment = $attmt1;
    $mypage->slug = str_slug($request->slug, '-');
    $mypage->menu_name = $request->menu_name;
    $mypage->save();

    return redirect()->route('menupages.index')->with('message', 'Page updated successfully.');
}

查看文件(底部):

                                <label for="attachement1">Attach a file: <small style="color:#999;">(type: zip/rar and below 10mb)</small></label>

                            <input type="file" name="file1"/><br/>
                            <label for="snapshot">Snapshot / Thumbnail:</label>

                            <input type="file" name="thumbnail" required/><br/>

                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input name="_method" type="hidden" value="PUT">

                            <input type="submit" class="btn btn-primary" name="Submit" value="Publish" />