我收到一个错误,它似乎是一个语法错误?我真的不确定我错在哪里了?
public function upload() {
// getting all of the post data
$file = array('image' => Input::file('image'));
// setting up rules
$rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
return Redirect::to('upload')->withInput()->withErrors($validator);
}
else {
// checking file is valid.
if (Input::file('image')->isValid()) {
$destinationPath = 'uploads'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
// sending back with message
Session::flash('success', 'Upload successfully');
return Redirect::to('upload');
}
else {
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('upload');
}
}
}发布于 2015-02-01 07:33:29
您需要一个class上下文才能使用访问标识符。
你的代码应该是:
class MyClass {
public function upload() {
// getting all of the post data
$file = array('image' => Input::file('image'));
// setting up rules
$rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
return Redirect::to('upload')->withInput()->withErrors($validator);
}
else {
// checking file is valid.
if (Input::file('image')->isValid()) {
$destinationPath = 'uploads'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
// sending back with message
Session::flash('success', 'Upload successfully');
return Redirect::to('upload');
}
else {
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('upload');
}
}
}
}发布于 2015-02-01 07:26:05
如果你不在一个类中,就使用function upload(),而不是public function upload()。
如果你在一个类里面,错误就在你提供的代码之前。
https://stackoverflow.com/questions/28257692
复制相似问题