【问题标题】:Unable to upload image through form?无法通过表单上传图片?
【发布时间】:2018-05-29 08:10:44
【问题描述】:

这是我的 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 格式)并单击提交时,我收到“缩略图必须是以下类型的文件:jpg、jpeg、png。”我检查了数据库,没有记录文件。

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

【问题讨论】:

  • 回归基础,你在form标签中实现enctype = multipart/form-data了吗??
  • 是的,这是代码的开头:&lt;form class="form-horizontal" role="form" method="post" action="savepost" enctype="multipart/form-data"&gt;
  • 尝试 var_dump() 检查文件类型和发布的请求
  • 错误:“var_dump() 需要至少 1 个参数,给定 0”我使用了 &lt;?php echo var_dump(); ?&gt;
  • 再次将文件另存为 jpg 并重试。该文件可能是 GIF 并手动重命名为 JPG

标签: php html laravel-5


【解决方案1】:

您是否在表单上添加了 enctype="multipart/form-data"?

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

 </form

【讨论】:

  • 是的,这是代码的开头:&lt;form class="form-horizontal" role="form" method="post" action="savepost" enctype="multipart/form-data"&gt;
【解决方案2】:

当你想上传一些东西时,你总是需要在你的表单中添加以下代码。

enctype="multipart/form-data"

如果您不这样做,您将无法上传内容。 您是否将此添加到您的 html 表单中?

【讨论】:

  • 看看我上面的cmets。
  • 您能否提供完整的代码,以及按下提交按钮和调用 php 的部分?
【解决方案3】:

我从开发人员那里得到了帮助,所以我将发布我们是如何解决这个问题的。

这是函数的完整修改代码:

控制器:

  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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2020-06-07
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多