2015-06-16 63 views
1

我正在创建用于更新用户配置文件数据的REST API。我创建了一个API我想用它来更新所有类型的数据,如名称,电子邮件和个人资料图片。如何在php中使用put方法上传图片?

如果我没有发送多部分请求(意味着我只是更新文本),API的工作正常,但如果我更新图像,然后我看到一个问题 - 我无法更新个人资料图片。但是如果我使用POST方法,那么我可以更新配置文件pic &数据。

这里是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); 
    }); 

请建议做什么码?

回答