【发布时间】:2014-12-06 20:34:20
【问题描述】:
所以我使用干预包来保存并调整我的图像,如果它需要大分辨率,但是当我尝试上传大图像(大约 10 mb)时我得到 MethodNotAllowedHttpException。
所以我在这里和网上做了一些研究,确实有一些答案,但没有一个对我有用,我想知道为什么会这样?
try{
$img = Input::file('gambar');
if(!empty($img)){
$file_max = ini_get('upload_max_filesize');
$file_max_str_leng = strlen($file_max);
$file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
$file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
$file_max = substr($file_max,0,$file_max_str_leng - 1);
$file_max = intval($file_max);
$filename = $img->getClientOriginalName();
$size = $img->getSize();
if ($size < $file_max){
if($this->save_image($img,$artikel,$filename)){
$artikel->update(array(
'judul' => $judul,
'content' => Input::get('content'),
'kategori' => Input::get('kategori'),
'status' => Input::get('status'),
'pilihan' => Input::get('pilihan'),
'gambar' => $filename
));
}else{
return Redirect::back()->withErrors($validator)->withInput();
}
}else{
return Redirect::back()->withInput()->with('errormessage','El tamaño del archivo debe ser menor que %smb.',$file_max);
}
}
}catch(Exception $e){
return Redirect::back()->withInput()->with('errormessage','The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit);
}
这是我的 save_image 函数
function save_image($img,$artikel,$filename){
list($width, $height) = getimagesize($img);
$path = public_path('images_artikel/');
File::delete($path . $artikel->gambar);
if($width > 1280 && $height > 720){
if(Image::make($img->getRealPath())->resize('1280','720')->save($path . $filename))
return true;
else
return Redirect::back()->withInput()->with('errormessage','Terjadi kesalahan dalam penyimpanan');
}else{
if(Image::make($img->getRealPath())->save($path . $filename))
return true;
else
return Redirect::back()->withInput()->with('errormessage','Terjadi kesalahan dalam penyimpanan');
}
}
我也尝试在我的模型中使用验证规则,但不起作用...
private $file_max;
function _construct(){
$file_max = ini_get('upload_max_filesize');
$file_max_str_leng = strlen($file_max);
$file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
$file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
$file_max = substr($file_max,0,$file_max_str_leng - 1);
$file_max = intval($file_max);
}
// Add your validation rules here
public static $rules = [
'judul' => 'required|between:5,255',
'content' => 'required',
'gambar' => 'image|mimes:jpeg,jpg,png,bmp|max:{$file_max}'
];
这是我的路线
Route::resource('artikels','AdminArtikelsController');
还有我的表格
{{ Form::model($artikel, array('route' => array('admin.artikels.update',$artikel->id), 'method' => 'put', 'files' => true)) }}
那么还有其他解决方案吗?
【问题讨论】:
标签: php file-upload laravel laravel-4