PHP将图像上传到Laravel控制器,该控制器将其作为字符串读取?

时间:2018-10-31 17:43:09

标签: php laravel image eloquent upload

在Laravel刀片中,我正在编写一个表单,某些用户可以在其中将一本书的详细信息添加到数据库中。下面是HTML:请注意“照片”部分。

<form action="submitAdd" method="get" class="form-inline" enctype="multipart/form-data">
  @csrf
  <div class="form-group">
    <label for="title ">Title: </label>
      <input type="text" name="title " >
    <label for="author">Author: </label>
      <input type="text" name="author">
  </div>

  <div class="form-group">
    <label for="book_id">Book ID: </label>
      <input type="number" name="book_id">
  </div>

  <div class="form-group">
    <label for="genre_id">Genre: </label>
      <select name="genre_id">
        <option name="Arts" value=1>Arts</option>
        <option name="Engineering" value=2>Engineering</option>
        <option name="Chemistry" value=3>Chemistry</option>
        <option name="Philosopy" value=4>Philosophy</option>
        <option name="Medicine" value=5>Medicine</option>
      </select>
  </div>

  <div class="form-group">
    <label for="status_id">Status: </label>
      <select name="status_id">
        <option name="Available" value=1>Available</option>
        <option name="Requested" value=2>Requested</option>
        <option name="Unavailable" value=3>Unavailable</option>
        <option name="Alumni" value=4>Alumni</option>
      </select>
  </div>

  <div class="form-group">
    <label for="photo">Photo: </label>
      <input type="file" name="photo" />
  </div>

  <div class="form-group">
    <input type="submit" name="submit" value="Add Book" />
  </div>
</form>

这将调用下面列出的控制器方法“ AddBook”

public function addBook(Request $request) {

    $genres = [1, 2, 3, 4, 5];
    $statuses = [1,2,3,4];

    $validated = Validator::make($request->all(), [
      "submit"    => "required",
      "book_id" => ["required", "integer", "regex:/^[0-9]+$/"],
      "title" => ["required", "regex: /^[a-zA-Z’'. -]+$/"],
      "author"   => ["required", "regex: /^[a-zA-Z’'. -]+$/"],
      "genre_id" => ["required", Rule::in([1, 2, 3, 4, 5]) ],
      "status_id" => ["required", Rule::in([1, 2, 3, 4]) ],
      "photo" => "required|image|file:jpeg,png,jpg,gif,svg|max:2048"
    ]);

    $errors = $validated->errors();

    if($validated->fails()) {
      return redirect()->back()->withInput($request->all())->withErrors($errors);
    }

    $data = $request->all();
    $image = "";
    if ($request->hasFile("photo")) {
      echo "<h2>". $request->photo . "</h2>";
      $image = $request->file('photo');
      $new_name = rand() . '.' . $image->getClientOriginalExtension(); //CAUSES ERROR
      $image->move(public_path('photos'), $new_name);
    } else {
      echo "<h3>Request doesn't have photo</h3>";
    }
    try {
      $insert =
      DB::table('CCEAGpoc.dbo.Student')->insert([
        ['student_id'  => $data['student_id'],
        ‘title'    => $data['title'],
        'author'     => $data['author'],
        'genre_id'   => $data['genre_id'],
        'photo'       => $image, 
        'status_id'   => $data['status_id']]
      ]);
        return view ('submit/submitSuccess/addSuccess');//->with('message', $message);

    } catch (Exception $ex) { 
      return redirect()->back()->withInput($request->all())->withErrors($errors);
    }
  }

以上代码适用于除照片之外的所有内容。

提交时,出现以下验证失败:

“照片必须是图像。 照片必须是文件。”

删除照片验证后,我会收到以下错误消息:

"SQLSTATE[HY000]: General error: 20018 Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. [20018] (severity 16) [insert into [CCEAGpoc].[dbo].[Student] ([course_id], [forename], [photo], [status_id], [student_id], [surname]) values ('2', 'w', '', '3', '10000029', 'w')] (SQL: insert into [CCEAGpoc].[dbo].[Student] ([course_id], [forename], [photo], [status_id], [student_id], [surname]) values (2, w, , 3, 10000029, w))"

当我删除执行照片检查的if-check时,出现此错误:

Call to a member function getClientOriginalExtension() on null

对于此行:

$new_name = rand() . '.' . $image->getClientOriginalExtension();

我已经找到了解决这些错误的方法,但是它们并没有帮助我。我还应该做什么?

1 个答案:

答案 0 :(得分:1)

您正在使用GET形式方法,应该使用POST