【问题标题】:Laravel 5.2 - Upload Files to DatabaseLaravel 5.2 - 上传文件到数据库
【发布时间】:2017-04-24 09:33:56
【问题描述】:

这些是我所拥有的:

名为lamanInformasi的数据库表,具有以下字段:idjudulisicreated_atupdated_at

这就是我想要的:

用户可以上传多个文档或图像文件,这些文件将被存储到数据库中。文件名将保存到isi 字段,文件本身将保存到名为propic 的文件夹中。用户还可以在网站上显示数据库中的所有数据。

这些是我的代码:

create.blade.php

<form action="lamanInformasiController@index" method="post" enctype="multipart/form-data">
    <input type="file" name="image"><br />
    <input type="submit" name="submit" value="Submit">
</form>

lamanInformasiController.php

public function index(Request $request)
{
    $file = new file;
    if (Input::hasFile('image'))
    {
        $destinationPath = public_path().'/propic/';
        $name = Input::file('image')->getClientOriginalName();
        $extension = Input::file('image')->getClientOriginalExtension();

        $file = Input::file('image')->move($destinationPath, $name . "." . $extension);
    }
    $file -> isi = $request->get($file);
    $file -> save();

    $lamanInformasi = LamanInformasi::all();
    return view('upload.index', compact('lamanInformasi'));
}

index.blade.php

<table class="table table-striped table-bordered" border= "1px solid black">
    <thead>
        <tr>
            <td>ID</td>
            <td>Judul</td>
            <td>Isi</td>
            <td>Created At</td>
            <td>Updated At</td>
        </tr>
    </thead>
    <tbody>
        @foreach($$lamanInformasi as $key => $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->judul}}</td>
            <td>{{$value->isi}}</td>
            <td>{{$value->created_at}}</td>
            <td>{{$value->updated_at}}</td>
         </tr>
         @endforeach
    </tbody>
</table>

当我运行它时,我有这个错误:

ErrorException in ParameterBag.php line 90:
array_key_exists(): The first argument should be either a string or an integer

我有这个在ParameterBag line 89-91

public function get($key, $default = null)
{
    return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

这些是我的问题:

如何解决该错误?我是否使代码正确上传文件?因为我尝试过类似的代码,但它不起作用。谢谢

【问题讨论】:

  • array_key_exists 处理 null、float、boolean 和 'integer-representing string' 键的方式本身是不一致的,在 bool 和 float 的情况下,与它们在用作数组时的转换方式不一致偏移量。
  • @FullStack 对不起,我不明白。我该怎么办?
  • @FullStack 我还是不明白我的 ParameterBag 出了什么问题

标签: php database laravel file-upload laravel-5.2


【解决方案1】:

您需要注意几件事。试试下面的代码

LamanInformasiController.php - 控制器名称通常大写

class LamanInformasiController extends Controller
{
    /**
     * @var LamanInformasi - include the use statement above for the model.
     */
    protected $model;

    /**
     * Inject (model)LamanInformasi while instantiating the controller.
     * @param LamanInformasi $model
     */
    public function __construct(LamanInformasi $model)
    {
        $this->model = $model;
    }

    public function index()
    { 
        $lamanInformasi = $this->model->all();
        return view('upload.index', compact('lamanInformasi'));
    }


    public function store(Request $request)
    {
        if (Input::hasFile('image'))
        {
            $destinationPath = public_path().'/propic/';
            $name = Input::file('image')->getClientOriginalName();
            $extension = Input::file('image')->getClientOriginalExtension();
            $fileName = $name.'.'.$extension;

            //store the file in the $destinationPath
            $file = Input::file('image')->move($destinationPath, $fileName);

            //save a corresponding record in the database
            $this->model->create(['isi'=> $fileName]);

            //return success message
        } 
        //return failure message
    }

}   //don't forget to include the use statement for Input or write \Input

然后在你的 index.blade.php

<table class="table table-striped table-bordered" border= "1px solid black">
<thead>
    <tr>
        <td>ID</td>
        <td>Judul</td>
        <td>Isi</td>
        <td>Created At</td>
        <td>Updated At</td>
    </tr>
</thead>
<tbody>
    @foreach($lamanInformasi as $file)
    <tr>
        <td>{{$file->id}}</td>
        <td>{{$file->judul}}</td>
        <td>{{$file->isi}}</td>
        <td>{{$file->created_at}}</td>
        <td>{{$file->updated_at}}</td>
     </tr>
     @endforeach
</tbody>

你的表单动作应该是相应的

<form action="/upload8" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br />
<input type="submit" name="submit" value="Submit">

这应该可行,但尚未测试。否则请告诉我。

【讨论】:

  • 我犯了一个错误。我应该把代码放在LamanInformasiControllerstore 函数中的index 函数中,所以我移动了它。它工作正常,直到我选择一个文件并单击提交,我有这个错误NotFoundHttpException in RouteCollection.php line 161
  • 我有这个在routes.phpRoute::resource('/upload8','LamanInformasiController');
  • 如果您使用的是Route::resource,那么您应该遵循 REST 约定并在您的 LamanInformasiControllerstore 方法中处理上传文件的存储并更改表单操作
  • 你能给出正确的代码吗?我不知道我必须改变哪一部分
  • form action 转到错误的 URL。我使用以下 URL 访问表单:http://localhost/wfpProject/public/upload8。当我点击提交时,它会将我带到这个 URL:http://localhost/upload8
猜你喜欢
  • 2017-04-18
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2016-06-09
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多