skip to Main Content

Any idea why PATCH wouldn’t be accepting multipart/form-data file uploads? I am trying to update data using mumultipart form data but my postman is not recognising variables If I POST the file, it works fine. following is my function in php

function updateImage($writeDB, $returned_userid) {
 
  // Retrieve the record to be updated
  $query = $writeDB->prepare('SELECT users.id, users.name, users.mobilenumber, users.profilepic from users where users.id=:userid');

  $query->bindParam(':userid', $returned_userid, PDO::PARAM_INT);
  $query->execute();

  // get row count
  $rowCount = $query->rowCount();

  // make sure that the user exists for the given ID
  if($rowCount === 0) {
    // send JSON error response using function
    sendResponse(404, false, "No user found to update");
  }

  // for each row returned - should be just one
  while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $image = new Image($row['id'], null, $row['name'], $row['mobilenumber'], $row['profilepic'], null, null, null, null, null, null, null, null);
  }

  // Update the record
  $name = isset($_POST['name']) ? $_POST['name'] : null;
  $filename = isset($_FILES['image']['name']) ? $_FILES['image']['name'] : null;
  $tmp_name = isset($_FILES['image']['tmp_name']) ? $_FILES['image']['tmp_name'] : null;

  if (!empty($name)) {
     echo $name;
    $image->setProfile($name);
  }

  if (!empty($filename) && !empty($tmp_name)) {
    // Handle the image upload
    move_uploaded_file($tmp_name, "uploads/$filename");
    $image->setProfilePic("uploads/$filename");
  }

  // Update the database record
  $query = $writeDB->prepare("UPDATE users SET profilepic = :profilepic WHERE id = :userid");
  $query->bindParam(':profilepic', $image->getProfilePic());
  $query->bindParam(':userid', $returned_userid, PDO::PARAM_INT);
  $query->execute();

  // Return the updated record
  $record = array(
    "id" => $image->getId(),
    "name" => $image->getUsername(),
    "mobilenumber" => $image->getMobileNo(),
    "profilepic" => $image->getProfilePic()
  );

  header('Content-Type: application/json');
  echo json_encode($record);
}
if(empty($_GET)){
         $returned_userid = checkAuthStatusAndReturnUserID($writeDB);
        switch($_SERVER['REQUEST_METHOD']){
        case 'PATCH':
  
        updateImage($writeDB, $returned_userid);
            break;

    }
    }

following is my postman response I am trying to print the name variable but it is always null

enter image description here

2

Answers


  1. PATCH is not for uploading binary data (like what you want with the image), see: https://developer.mozilla.org/en-US/docs/web/http/methods/patch
    (Allowed in HTML Forms: No).

    Uploading binary data (like images) has to be done with POST always, PUT or PATCH are used to modify the attributes of the ressorce.

    Login or Signup to reply.
  2. You can only retrieve data directly from $_POST if the request method is post. Since the request method is patch in your case, you can parse the data to $_POST like so

    parse_str(file_get_contents('php://input'), $_POST);
    echo $_POST['name']; // now this should work
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search