【问题标题】:$request->file('logoImg') always null$request->file('logoImg') 总是为空
【发布时间】:2019-07-01 18:44:24
【问题描述】:

基本上我有一个包含文件输入类型的表单,当提交表单时,所有输入都返回他的内容,除了文件输入。

在同一个论坛的大多数回复中,我发现我需要添加属性,我已经启用了 php 中的文件上传、max_file_size over 512 mb 等,但没有任何结果。

当我检查 $request->input('logoImg') 时,检索我在表单中选择的文件的名称,但如果我更改为 $request->file('logoImg'),则需要任何可上传的文件文件对象,但返回 NULL。

我对此非常着迷,我相信问题出在 Laravel 配置环境中,重复一遍,我相信。

这是我的代码。

这里是xxxx.blade.php文件中的表单代码

<form method="POST" action="{{ route ('socioActualizar', $socio->Id) }}" enctype=”multipart/form-data”>



      <div class="row" style="padding:15px;">


         <!-- Main content for option-->
          <div class="card shadow mb-4" style="width:100%">
            <div class="card-header py-3">
              <h6 class="m-0 font-weight-bold text-primary">Socio: {!! $socio->Nombre_Apellidos !!}</h6>
              <button type="button"style="background:skyblue; float:right; padding:5px; margin-top:5px;" onclick="location.href = '{{ route('cuotas',$socio->Id) }}'">Ver cuotas</button>
            </div>

          </div>

    <!-- /.container-fluid -->
            <div class="card shadow mb-4" style="width:50%; padding:15px; float:left;">
            <div class="card-header py-3">
              <h7 class="m-0 font-weight-bold text-primary">Datos Personales</h7>
            </div>
            {{ csrf_field() }}
            <label for="Numero_Socio">Número de socio</label>
            <input type="number" name="Numero_Socio" id="Numero_Socio" value="{{ $socio->Numero_Socio }}" disabled style="text-align:right; width:25%; padding:5px;">

            <label for="Nombre_Apellidos">Nombre y Apellidos</label>
            <input type="text" name="Nombre_Apellidos" id="Nombre_Apellidos" value="{{ $socio->Nombre_Apellidos }}" style="text-align:left; width:100%; padding:5px;">

            <label for="DNI">Dni / Nif</label>
            <input type="text" pattern="[0-9A-Za-z]{9}" placeholder="Formato 12345678X" name="DNI" id="DNI" value="{{ $socio->DNI }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Direccion">Dirección</label>
            <textarea name="Direccion" id="Direccion" rows="5" cols="1" style="text-align:left; width:100%; padding:5px;">{{ $socio->Direccion }}</textarea>

            <label for="Poblacion">Población / Municipio</label>
            <input type="text" name="Poblacion" id="Poblacion" value="{{ $socio->Poblacion }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Calle">Provincia</label>
            <input type="text" name="Calle" id="Calle" value="{{ $socio->Calle }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Cp_Provincia" style="width:100%;">Código postal</label>
            <input type="number" name="CP_Provincia" id="CP_Provincia" value="{{ $socio->CP_Provincia }}" style="text-align:left; width:25%; padding:5px;">

            <label for="Correo_Electronico" style="width:100%;">Correo Electrónico</label>
            <input type="email" name="Correo_Electronico" id="Correo_Electronico" value="{{ $socio->Correo_Electronico }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Telefono" style="width:100%;">Teléfono</label>
            <input type="text" name="Telefono" id="Telefono" value="{{ $socio->Telefono }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Fecha_Alta" style="width:100%;">Fecha de alta</label>
            <input type="date" name="Fecha_Alta" id="Fecha_Alta" value="{{ $socio->Fecha_Alta }}" disabled style="text-align:left; width:30%; padding:5px;">

            <label for="Fecha_Baja" style="width:100%;">Fecha de baja</label>
            <input type="date" name="Fecha_Baja" id="Fecha_Baja" value="{{ $socio->Fecha_Baja }}" style="text-align:left; width:30%; padding:5px;">




    </div>

    <div class="card shadow mb-4" style="width:50%; padding:15px; float:left;">
            <div class="card-header py-3">
              <h7 class="m-0 font-weight-bold text-primary">Datos Profesionales</h7>
            </div>
            <label for="Quien_lo_Presenta">Quién lo presenta</label>
            <input type="text" name="Quien_Lo_Presenta" id="Quien_Lo_Presenta" value="{{ $socio->Quien_Lo_Presenta }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Cargo">Cargo</label>
            <input type="text" name="Cargo" id="Cargo" value="{{ $socio->Cargo }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Ejercito">Ejército</label>
            <input type="text" name="Ejercito" id="Ejercito" value="{{ $socio->Ejercito }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Cuerpo_Servido">Cuerpo Servido</label>
            <input type="text" name="Cuerpo_Servido" id="Cuerpo_Servido" value="{{ $socio->Cuerpo_Servido }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Tercio_Brigada">Tercio Brigada</label>
            <input type="text" name="Tercio_Brigada" id="Tercio_Brigada" value="{{ $socio->Tercio_Brigada }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Bandera">Bandera</label>
            <input type="text" name="Bandera" id="Bandera" value="{{ $socio->Bandera }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Compañia">Compañía</label>
            <input type="text" name="Compañia" id="Compañia" value="{{ $socio->Compañia }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Localidad">Localidad</label>
            <input type="text" name="Localidad" id="Localidad" value="{{ $socio->Localidad }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Provincia">Provincia</label>
            <input type="text" name="Provincia" id="Provincia" value="{{ $socio->Provincia }}" style="text-align:left; width:100%; padding:5px;">

            <label for="Fecha_Ingreso" style="width:100%;">Fecha de ingreso</label>
            <input type="date" name="Fecha_Ingreso" id="Fecha_Ingreso" value="{{ $socio->Fecha_Ingreso }}" style="text-align:left; width:30%; padding:5px;">

            <label for="Fecha_Licencia" style="width:100%;">Fecha de Licencia</label>
            <input type="date" name="Fecha_Licencia" id="Fecha_Licencia" value="{{ $socio->Fecha_Licencia }}" style="text-align:left; width:30%; padding:5px;">


    </div>
       <div class="card shadow mb-4" style="width:30%; padding:15px; float:left;">
           @if ($socio->Imagen == '')
                <img id="avatar" src="../img/soldier.png" style="width:125px; height:125px;">
           @else
                <img id="avatar" src="{{ $socio->Imagen }}" style="width:125px; height:125px;">
           @endif

           <input type="file" name="logoImg" id="Img" style="padding:5px; margin-top:5px;" onchange="showMyImage(this)" >

        </div>

      <div class="card shadow mb-4" style="width:70%; padding:15px; float:left;">

            <label for="Observaciones">Observaciones</label>
            <textarea name="Observaciones" id="Observaciones" rows="8" cols="1" style="text-align:left; width:100%; padding:5px;">{{ $socio->Observaciones }}</textarea>

        </div>



     <!-- Main content for option-->
          <div class="card shadow mb-4" style="width:100%; height:55px;">

           <input type="submit" Value="Guardar Cambios" id="guardar_cambios" style="background:skyblue; position:absolute; right:10px; padding:5px; margin-top:5px;">

          </div>



  </div>
  </form>

这是我的路由 web.php 文件

Route::get('/socios', 'SociosController@sociosView')->name('socios');
Route::get('/socio/{id}','SociosController@sociosSelect')->name('socio');
Route::post('/socioActualizar/{id}','SociosController@socioActualizar')-> name('socioActualizar');
Route::get('/cuotas/{id}','CuotasController@verCuotasSocio')->name('cuotas');

最后,控制器中的代码

 public function socioActualizar(Request $request, $id)
{
    $socio = Socio::where('Id',$id)->first();
    $socio->Nombre_Apellidos = $request->input('Nombre_Apellidos');
    $socio->DNI = $request->input('DNI');
    $socio->Direccion = $request->input('Direccion');
    $socio->Calle = $request->input('Calle');
    $socio->Poblacion = $request->input('Poblacion');

    $socio->CP_Provincia = $request->input('CP_Provincia');
    $socio->Telefono = $request->input('Telefono');
    $socio->Correo_Electronico = $request->input('Correo_Electronico');

  //  $socio->Fecha_Alta = $request->input('Fecha_Alta');
    $socio->Fecha_Baja = $request->input('Fecha_Baja');

    $socio->Quien_Lo_Presenta = $request->input('Quien_Lo_Presenta');
    $socio->Cargo = $request->input('Cargo');
    $socio->Ejercito = $request->input('Ejercito');
    $socio->Cuerpo_Servido = $request->input('Cuerpo_Servido');
    $socio->Tercio_Brigada = $request->input('Tercio_Brigada');
    $socio->Bandera = $request->input('Bandera');
    $socio->Compañia = $request->input('Compañia');
    $socio->Localidad = $request->input('Localidad');
    $socio->Provincia = $request->input('Provincia');
    $socio->Fecha_Ingreso = $request->input('Fecha_Ingreso');
    $socio->Fecha_Licencia = $request->input('Fecha_Licencia');
    $socio->Observaciones = $request->input('Observaciones');
    //logica de la subida de imagen en laravel

    //obtenemos el campo file definido en el formulario
    $name = $request->input('logoImg'); // returns name of file selected
    $name = $request->file('logoImg'); // returns null Why??
    $request->logoImg->store('uploads');
    if($request->hasFile('logoImg')){
        $logoImage = $request->file('logoImg');
        $name = $logoImage->getClientOriginalName();
        //indicamos que queremos guardar un nuevo archivo en el disco local
        Storage::disk('local')->put($name,  File::get($logoImage));
    }

    //obtenemos el nombre del archivo
    $socio->Imagen = $name;
    $socio->save();

    return Redirect::to('socios/');
}

我在这个看似简单的步骤中太迷失了,但不起作用,肯定是一个小错误,但我找不到它。

非常感谢。

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    我对您的问题没有具体的答案,但我解决您问题的方法是暂时离开您的框架并使用纯 PHP 进行调试。

    在这种情况下,我会查看您的$_FILES superglobal 并确定您的文件是否正确上传。

    如果您没有收到任何内容,那么我会查看您的 &lt;form&gt; 和 Chrome 中的检查器,以确保您的文件被正确发送。我不知道是不是你的复制粘贴,但你 enctype 上的引号看起来很可疑。

    【讨论】:

    • $_FILES 返回 [](空数组),但我会更改表单属性中的引号并对其进行测试....
    • 你太棒了!,这就是问题所在,引号,我已经更改为简单 (') 并且可以完美运行。天哪,我还有很多东西要学。好的,问题解决了。
    • @MarioRodríguez $file = $request->file('photo'); 在您修复 qoutes 后为您工作了吗?还是您使用 $_FILES 超全局?
    【解决方案2】:

    这里我复制了我用来上传图片的代码。
    为了创建缩略图并保存图像,我使用了 Intervention Image 一个 PHP 图像处理和操作库。

    这里解释了如何安装它:
    http://image.intervention.io/getting_started/installation

     if ($request->file('image')){
          $imageFile = $request->file('image');
          $imageName = time() . '.' . 'jpg';  //$imageName = $teaserImageFile->hashName();
          $imageSubdir = "events_teaser";
          $imageWidth = "968";
          $thumbWidth = "310";
    
         $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
          $event->image = $imageName;
               }
    
    
    // **********************************************************************
        /**
         * Upload image on server
         *
         * @param  $imageFile - the file to upload
         * @param  $imageName - the file name
         * @param  $imageSubdir - the subdir in /storage/app/public/images/..
         * @return none
         */
        function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth){
    
            // Create dir if not exist (in /storage/app/public/images/..)
                if(!\Storage::disk('public')->has('images/'.$imageSubdir.'/')){
                    \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
                }
    
                $destinationPath = "app/public/images/".$imageSubdir."/";
    
                // Resize the image with Intervention - http://image.intervention.io/api/resize
                    // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
                    // - save file as jpg with medium quality
                    $image = \Image::make($imageFile->getRealPath())
                                    ->resize($imageWidth, null, 
                                        function ($constraint) {
                                            $constraint->aspectRatio();
                                    })
                                    ->save(storage_path($destinationPath . $imageName), 75); 
    
                // Create the thumb            
                    $image->resize($thumbWidth, null, 
                        function ($constraint) {
                            $constraint->aspectRatio();
                    })
                    ->save(storage_path($destinationPath . "thumb_".$imageName), 75); 
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-04
      • 2016-07-15
      • 2015-12-24
      • 2021-08-12
      • 2020-02-10
      • 2011-07-22
      相关资源
      最近更新 更多