php - Laravel 文件上传只需将文件名发送到 Controller

标签 php laravel file laravel-5

我正在尝试上传图像。

这是我的 html 表单:

<form class="form" action="{{ URL::to('/alunos/cadastrar') }}" method="post" enctype=”multipart/form-data”>
    <label for="nome"  >Nome</label>
    <input  type="text" name="nome" placeholder="nome"><br><br>

    <label for="serie"  >Série</label>
    <input  type="text" name="serie" placeholder="serie"><br><br>

    <label for="escola"  >Escola</label>
    <input  type="text" name=escola placeholder="escola"><br><br>

    <label for="turno"  >Turno</label>
    <input  type="text" name=turno placeholder="Turno"><br><br>

    <label for="documento"  >Documento</label>
    <input  type="text" name=documento placeholder="documento"><br><br>

    <label for="residencia"  >Reside em</label>
    <input  type="text" name=residencia placeholder="Reside em"><br><br>

    <label for="rota"  >Rota</label>
    <input  type="text" name=rota placeholder="rota"><br><br>

    <label for="data_nasc"  >Data de Nascimento</label>
    <input  type="text" name=data_nasc placeholder="Data de Nascimnento"><br><br>

    <label for="mae"  >Mãe</label>
    <input  type="text" name=mae placeholder="Mãe"><br><br>

    <label for="pai"  >Pai</label>
    <input  type="text" name=pai placeholder="Pai"><br><br>

    <label for="photo"  >Foto</label>
    <input type="file" name="photo" id="photo">

    {{ csrf_field() }}

    <br>

    <input type="submit" value="Registrar!">
</form>

这是 Controller 方法:

public function submit(Request $request)
{
    if ($request->hasFile('photo')) {
        echo "OK";
    }else{
        echo "error";
    }
    return redirect('/');
}

每次运行时,都会收到错误消息。 dd(request()->all()) 给出以下内容。

array:12 [▼
  "nome" => null
  "serie" => null
  "escola" => null
  "turno" => null
  "documento" => null
  "residencia" => null
  "rota" => null
  "data_nasc" => null
  "mae" => null
  "pai" => null
  "photo" => "23722217_17536056.jpg"
  "_token" => "TPYBg3d8f3CdiQQnMbimprb5HQPkxqG5MSLdAUEa"
]

“照片”字段实际上没有文件,只有文件名。

我正在使用 MAMP 的 MacBook Pro 工作。在 Controller 代码的顶部,我有以下内容:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Quotation;
use Datatables;
use App\aluno;
use View;
use PDF;
use DB;

如何才能使此上传工作正常?

最佳答案

您的代码似乎是正确的。

唯一可能出现的问题是您的 enctype 有不同类型的引号。

尝试改变:

enctype=”multipart/form-data”

enctype="multipart/form-data"

关于php - Laravel 文件上传只需将文件名发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51680805/

相关文章:

javascript - 如何从另一个页面 JavaScript/PHP 获取 URL 值

c# - 如何创建没有标题的文件

postgresql - 如何按外键分组并在 laravel Eloquent 中检索其他列值

android - 在/sdcard 中创建目录失败

javascript - 如何在 Javascript 中读取和写入文件

php - 如何在 cakephp 3.x 中使用两个左连接

php - 使用 mysqli 的数组到字符串错误

php - android 不处理 JSON 对象

php - 带自定义选择/连接/顺序的 Laravel Eloquent 模型查询

laravel - Laravel Rest-API 端点的 HTTPS