I have a Form where among other info, a user would upload an image. I want to store the path to the image on the database and save the image to the public/img/
folder on the Server.
该表单是使用:{{ Form::open(['route'=>'pizzas.store', 'files'=>true]) }}
打开的,这样它就可以发布文件了.判断HTML时,我有以下情况:
<form method="POST" action="http://mypizza/pizzas"
accept-charset="UTF-8" enctype="multipart/form-data">
I can POST to my Controller and receive all the data from the Form as expected. The method handling the file is as follows:
public function store() {
//TODO - Validation
$destinationPath = '';
$filename = '';
if (Input::hasFile('image')) {
$file = Input::file('image');
$destinationPath = '/img/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$pizza = Pizza::create(['name' => Input::get('name'),
'price' => Input::get('price'),
'ingredients' => Input::get('ingredients'),
'active' => Input::get('active'),
'path' => $destinationPath . $filename]);
if ($pizza) {
return Redirect::route('pizzas.show', $pizza->id);
}
//TODO - else
}
当我 Select 一个文件并提交表单时,一切似乎都正常,除了/img
文件夹中没有保存任何文件.数据库正确注册文件路径和名称.
Running dd($uploadSuccess);
right after the if { ...}
block, I get the following:
object(Symfony\Component\HttpFoundation\File\File)#220 (2) {
["pathName":"SplFileInfo":private]=> string(17) "/img\YZmLw7_2.jpg"
["fileName":"SplFileInfo":private]=> string(12) "YZmLw7_2.jpg" }
What am I doing wrong?