5

So I got this simple HTML form

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

Which looks like this enter image description here

This allows me to upload an image and post it to the server where is it handled by the "upload.php" script.

upload.php looks like this

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

I would like to use Postman to send these post requests manually, but I don't how to make a proper request in postman.

So far I have tried like this enter image description here

But the PHP script doesn't recognise $_FILES["fileToUpload"]

I hope someone can tell/explain to me how I can use Postman so send a post request that can be handled by this PHP script.

If it's possible to do exactly this but then in JavaScript, I would love to know.

Have a great weekend :)

Mootje
  • 127
  • 9
pluto9800
  • 247
  • 3
  • 12
  • I think looking at this https://stackoverflow.com/questions/39037049/how-to-upload-a-file-and-json-data-in-postman/50531746 will be helpful. It describes doing POST request with postman including files. – Tom Groot Oct 18 '20 at 17:38

1 Answers1

4

Open your form on the browser (Chrome or Firefox).

Open the dev tools (F12) and go to the network tab.

Submit your form. You should see your request in the network tab.

Do a right click on the request and select Copy/ Copy as cURL (bash)

Copy/ Copy as cURL (bash)

Open Postman, click Import, select Raw text and paste what is in the clipboard from copying from Chrome.

Paste raw

Click Continue, and in Postman you will have the request.

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37