如何使用表单发布配置文件图片到苗条框架接受并将文件移动到文件夹?

时间:2016-03-27 11:08:11

标签: php rest slim

我正在使用一个发布个人资料图片的表单,然后是一个解析数据的函数文件,并将其发送到使用slim框架开发的休息服务。

这是我的代码:

form.php的:

<form action="<?php echo url('functions.php'); ?>" method="post" class="validate form-horizontal">
            <input type="hidden" name="action" value="save_account_settings">

<div class="form-group">
                    <label class="col-sm-3 control-label"><span class="colordanger">*</span>Profile picture</label>
                    <div class="col-sm-9">
                        <input type="file" name="profile_picture" class="form-control" value="<?php echo $user->profile_picture; ?>" data-rule-required="true">
                        <span class="help-block">Upload your profile picture here</span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-9 col-sm-offset-3">
                        <button type="submit" class="btn btn-primary" data-loading-text='<i class="fa fa-spinner fa-spin"></i> Saving...'><i class="fa fa-check"></i> Save</button>
                    </div>
                </div>
            </div>
        </form>

的functions.php:

case 'save_account_settings':


try {

                $input = array (
                "profile_picture" => "@" . post('profile_picture')
                );

                $extension = '/save_account_settings';
                $result = process_api_put($input,$base_url,$extension);
                msg($result->message, $result->status);

        } 

    catch (Exception $e) 
                {
                    msg($e->getMessage(), "danger");
                }



    break;

然后rest.php:

$app->put("/save_account_settings", function() use ($app)
{
    $input = $app->request()->getBody();
  $input = json_decode($input);
    try {
        if ($input->username && $input->email) 
        {
            //save the file
            $file_extension = pathinfo($input->profile_picture, PATHINFO_EXTENSION);
            $server_file_address = realpath('./') . '/uploads/profile_pictures/' . $_SESSION['id'] . date('Y-m-d H:i:s') . $file_extension;
            move_uploaded_file( $input->profile_picture, $server_file_address );
            //save to database
            $users = Model::factory('Users')->where('id', $_SESSION['id'])->find_one();
            $users->set('profile_picture', '/uploads/profile_pictures/' . $_SESSION['id'] . date('Y-m-d H:i:s') . $file_extension);
            $users->save();

        $status = "success";
        $message = 'Your settings have been saved successfully.';
        }
        else
        {
            $status = "danger";
            $message = 'Some error has occured. Please try again.';
        }

    } 

    catch (Exception $e) 
            {
                $status = "danger";
                $message = $e->getMessage();
            }
    $response = array(
        'status' => $status,
        'message' => $message
    );
    $app->response()->header("Content-Type", "application/json");
    echo json_encode($response);

});

它将文件名保存在数据库中,但不会将文件保存在uploads目录中。有没有人知道我哪里错了?

0 个答案:

没有答案