基本上,我有一个包含文件输入类型的表单,提交表单时,除文件输入外,所有输入都返回他的内容。
在同一论坛的大多数回复中,我发现我需要添加属性,我已经启用了 php 文件上传,max_file_size超过 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>
这是我的路由网络.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 图像处理和操作库。
下面解释了如何安装它:
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);
}
我对你的问题没有具体的答案,但我解决你的问题的方法是跳出你的框架一秒钟并使用普通的 PHP 进行调试。
在这种情况下,我会查看您的超全局$_FILES
并确定您的文件是否正确上传。
如果您没有收到任何内容,那么我会查看您在Chrome中的<form>
和检查器,以确保您的文件正确发送。我不知道这是否是你的复制粘贴,但你enctype
上的引号看起来很可疑。