表单文件输入不是POSTing

时间:2015-12-17 19:30:41

标签: php forms laravel

我有一个接受个人资料照片图像的表单。我知道表单正在工作,就其他字段而言,但似乎该文件甚至没有获得POST。当我回显输入时,正如你所看到的,我正在变为空。这是我第一次使用文件上传,所以也许我错过了file输入POST的方式。

这是在profile.blade.php中打开的表单

{!! Form::open(['id' => 'editUserProfileForm', 'class' => 'card', 'files' => true]) !!}

以下是profile.blade.php中的表单输入

<div class="form-group m-b-20">
    <span class="form-group-addon"><i class="zmdi zmdi-account"></i></span>
    <label for="profile_pic">Profile Photo</label>
    <input type="file" id="profile_pic" name="profile_pic" class="form-control">
</div>

这是带有验证的控制器方法。如果你看到一种更简洁的方式来做我正在做的事情,我很乐意接受建议。

public function postEditProfile($id)
    {
        $user = User::find($id);
        $user_information = [
            'name' => Input::get('name'),
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            'password_confirmation' => Input::get('password_confirmation'),
            'profile_pic' => Input::file('profile_pic'),
        ];
        $validator = Validator::make($user_information, [
            'name' => 'required',
            'email' => ($user_information['email'] != $user->email) ? 'required|unique:users' : 'required',
            'password' => (! empty($user_information['password'])) ? 'min:6' : '',
            'password_confirmation' => (! empty($user_information['password'])) ? 'same:password' : '',
            'profile_pic' => (! empty($user_information['profile_pic'])) ? 'mimes:jpeg,bmp,png' : '',
        ]);
        if($validator->fails()) {
            echo json_encode(['st' => 0, 'msg' => $validator->errors()->all()]);
        } else {
            ($user->name != $user_information['name']) ? $user->name = $user_information['name'] : false;
            ($user->email != $user_information['email']) ? $user->email = $user_information['email'] : false;
            (! empty($user_information['password'])) ? $user->password = bcrypt($user_information['password']) : false;
            if(! empty($user_information['profile_pic'])) {
                $profile_pic_destination = 'profile_img';
                $profile_pic_name = rand(11111111, 99999999) . '.' . $user_information['profile_pic']->getClientOriginalExtension();
                $user_information['profile_pic']->move($profile_pic_destination, $profile_pic_name);
                $user->photo = $profile_pic_name;
            }
            if($user->save()) {
                //echo json_encode(['st' => 1, 'msg' => 'Your profile was saved successfully. Boom.']);
                echo json_encode(['st' => 1, 'msg' => $user_information['profile_pic']]);
            } else {
                echo json_encode(['st' => 0, 'msg' => 'The user was not updated.']);
            }
        }
    }

以下是开发控制台中POST表单的屏幕截图: Google Chrome Dev Console

1 个答案:

答案 0 :(得分:0)

我很抱歉,但这似乎是AJAX的问题以及我提交表单的方式。我在这里找到了一个解决方案:

How can I upload files asynchronously?

相关问题