我尝试将图片文件上传到指定的文件夹中。
但是它显示了像这样的错误
FileException in UploadedFile.php line 235:
The file "download (1).jpg" was not uploaded due to an unknown error.
我的控制器功能是这样的
function postSave( Request $request)
{
$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
$data = $this->validatePost('tb_hotel');
$id = $this->model->insertRow($data , $request->input('hotel_id'));
if(!is_null(Input::file('logo')))
{
$updates = array();
$file = $request->file('logo');
$destinationPath = public_path().'/uploads/hotels/';
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension(); //if you need extension of the file
$newfilename = $id.'.'.$extension;
// print_r($newfilename);exit;
$uploadSuccess = $request->file('logo')->move($destinationPath, $newfilename);
if( $uploadSuccess ) {
$updates['logo'] = $newfilename;
}
$this->model->insertRow($updates , $id );
}
.
.
.
.
}
我不知道为什么!! ..我在另一个控制器中使用相同的功能,它可以正常工作..
但是在这里它向我显示了一个像我上面提到的错误..
有人请帮帮我..
由于
答案 0 :(得分:0)
也许使用已经创建的$文件并从文件名中删除某些字符对你来说很有用。
<?php
function postSave( Request $request) {
$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
$data = $this->validatePost('tb_hotel');
$id = $this->model->insertRow($data, $request->input('hotel_id'));
if (!is_null(Input::file('logo'))) {
$updates = array();
$file = $request->file('logo'); //<== YOU ALREADY HAVE THIS VARIABLE HERE....
$destinationPath = public_path() . '/uploads/hotels/';
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension(); //if you need extension of the file
//CLEAN UP THE FILE NAME...
$newfilename = trim( preg_replace("#[\(\)\[\]]#", "_", ($id . '.' . $extension)) );
$uploadSuccess = $file->move($destinationPath, $newfilename); //<== USE THE $file VARIABLE HERE...
if ($uploadSuccess) {
$updates['logo'] = $newfilename;
}
$this->model->insertRow($updates, $id);
}
}
}