how to upload image using put method in php?

1.2k Views Asked by At

I am creating a REST API for updating user profile data. I have created one API & I want to use it for updating all types of data such as name, email and profile picture.

If I am not sending multi-part request (means I am only updating text) the API works fine but if I update the image then I see a problem - I am not able to update the profile picture. But if I use POST method then I am able to update the profile pic & data as well.

Here is code for REST API

$app->put('/updateUser', 'authenticate', function() use($app)
     {


        global $user_id;
        $isFileUpdated=false;
        $file_path = "../uploads/";

        if (isset ( $_FILES ['files'] ))
        {

            $file_path = $file_path . basename( $_FILES['files']['name']);
            if(move_uploaded_file($_FILES['files']['tmp_name'], $file_path))
            {
                $isFileUpdated=true;

            }
        }


        $name = $app->request->put ( 'name' );
        $email = $app->request->put ( 'email' );
        $phone=$app->request->put( 'phone' );
        $password = $app->request->put ( 'password' );
        $address=$app->request->put ( 'address' );
        $language=$app->request->put ( 'language' );
        $profession=$app->request->put ( 'profession' );

        $db = new DbHandler();
        $response = array();

        // updating task
        $result = $db->updateUser($user_id, $name, $email,$isFileUpdated,$file_path,$phone,$password,$address,$language,$profession);
        if ($result) 
        {
            // task updated successfully
            $response["error"] = false;
            $response["message"] = "User updated successfully";
        }
        else 
        {
            // task failed to update
            $response["error"] = true;
            $response["message"] = "User failed to update. Please try again!";
        }
        echoRespnse(200, $response);
    });

Please suggest what to do?

1

There are 1 best solutions below

0
On

if you haven't done this...

call to 'PUT' method on older browser require you to customize/Override header to accept 'PUT' first, OR The easier way is to override 'POST' as 'PUT' by adding extra params of _METHOD=PUT http://www.slimframework.com/docs/objects/request.html