在我的Laravel 5应用程序中,管理员可以上传产品图片和产品的PDF文件。因此,表单有2个输入字段,如下所示:
我正在上传的文件(可以使用):
请帮助我。谢谢。
<div class="col-md-4 col-sm-6">
<div class="form-group">
{!! Form::label('image', 'Image File:') !!}
{!! Form::file('image', ['class' => 'form-control input-sm'] ) !!}
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="form-group">
{!! Form::label('leaflet', 'Leaflet:') !!}
{!! Form::file('leaflet', ['class' => 'form-control input-sm'] ) !!}
</div>
</div>
当我上传的图片和传单都小于2MB时,上传成功。但是当使用大于2MB的传单时,我遇到了TokenMismatchException at line 46
在我的php.ini
文件中,它位于/etc/php5/apache2/php.ini
,配置如下:
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2G
; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 6G
我正在上传的文件(可以使用):
- 图片: 名称:flower-1.jpg, 大小:
51.6kb
- PDF: 名称:productInfo.pdf, 大小:
777.2kB
- 图片: 名称:flower-1.jpg, 大小:
51.6kb
- PDF: 名称:productInfo-1.pdf, 大小:
5.00MB
public function update( $id, UpdateProductRequest $request ) {
$product = $this->prod->findProductById($id);
$this->prod->updateProductOfId($product->id, $request);
Flash::success('product_general_info_updated', 'The product general information has been successfully updated.');
return redirect()->back();
}
/**
* Coming from ProductRespository.php
*/
public function updateProductOfId($id, Request $request)
{
$prd = $this->findProductById($id);
$getAllInput = $request->all();
if($request->file('image'))
{
$imageType = array(
'product' => array(
'height' => 347,
'width' => 347
),
'category' => array(
'height' => 190,
'width' => 190
)
);
$imageFileName = $request->file( 'image' )->getClientOriginalName();
foreach ( $imageType as $key => $value )
{
$currentFile = Input::file( 'image' );
$fileName = $currentFile->getClientOriginalName();
$image = Image::make( $request->file( 'image' ) );
$name = explode( '.', $fileName );
$destinationPath = public_path().'/images/products/uploads';
if ( $key === 'product' ) {
$image->resize( $value[ 'width' ], $value[ 'height' ] );
$image->save( $destinationPath . '/' . $name[ 0 ] . "-" . $value[ 'width' ] . "-" . $value[ 'height' ] . ".jpg", 100 );
} elseif ( $key === 'category' ) {
$image->resize( $value[ 'width' ], $value[ 'height' ] );
$image->save( $destinationPath . '/' . $name[ 0 ] . "-" . $value[ 'width' ] . "-" . $value[ 'height' ] . ".jpg", 100 );
}
}
$getAllInput['image'] = $imageFileName;
}
if($request->file('leaflet'))
{
$currentFile = Input::file( 'leaflet' );
$fileName = $currentFile->getClientOriginalName();
$destinationPath = public_path().'/leaflets/products/uploads';
$currentFile->move($destinationPath, $fileName);
$getAllInput['leaflet'] = $fileName;
}
return $prd->update($getAllInput);
}
编辑1:
我正在使用表单模型绑定
,因此create
和edit
文件具有相同的表单:
<div class="container">
@include('errors.list')
{!! Form::open(['url' => '/admin/products', 'autocomplete' => 'off', 'files' => true]) !!}
@include('admin.products.product_general_form', ['submitButtonText' => 'Add Product'])
{!! Form::close() !!}
</div>
EDIT 2:
仅供参考,我正在使用Ubuntu 14.04 LTS x64位架构上的LAMP。这是本地主机。我尚未托管应用程序。请帮助我。谢谢。
ini_get('upload_max_filesize')
这样的方法来检查你的最大上传文件大小是否已经被配置文件正确设置了吗? - Yoann Chambonnet