0

I am trying to HTTP push files to a server from Powershell on my Windows machine. The server is using a PHP script (cut down from the w3schools version here: https://www.w3schools.com/php/php_file_upload.asp):

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

Using the HTML file shown on the w3schools tutorial and Chrome as a browser, I am able to upload using this script with no problems. However, I can't seem to upload using Invoke-WebRequest.

Here is the command I am using:

Invoke-WebRequest -InFile "C:\test.txt" -ContentType "text/plain" -Method post -Uri http://example.com/upload/upload.php

And a segment of the output:

StatusCode        : 200
StatusDescription : OK 
Content           : Sorry, there was an error uploading your file.

What am I doing wrong?

  • `-InFile` will consume the whole POST body. PHP won't see any `$_FILES` unless the request came in with a `multipart/form-data` payload however. – mario Jan 28 '19 at 04:01
  • @mario, how do I achieve this? That post does cover a similar issue, but I cannot see how the answer relates to my issue. Apologies, but I truly have no idea what I am doing. – Connor Varney Jan 28 '19 at 04:12
  • @ConnorVarney try to use `Invoke-RestMethod` – HariHaran Jan 28 '19 at 04:23
  • PD of https://stackoverflow.com/questions/25075010/upload-multiple-files-from-powershell-script, but see example 4 in the MSDN docs for the more contemporary approach. – mario Jan 28 '19 at 04:26
  • @HariHaran tried two ways with Invoke-RestMethod, but with no luck. Commands I tried are here: https://pastebin.com/g4EP8XbV – Connor Varney Jan 28 '19 at 04:28
  • @mario - By example 4 you mean the one that uses the -Form argument? – Connor Varney Jan 28 '19 at 04:33
  • No. The one with `-Body` populated from a `[System.Net.Http.MultipartFormDataContent]::new()` – mario Jan 28 '19 at 04:36
  • My powershell version (5.1) does not seem to support it? I receive `Unable to find type [System.Net.Http.Headers.ContentDispositionHeaderValue]. ` – Connor Varney Jan 28 '19 at 04:41
  • Might be PSCore/6 then. Use one of the awful manual MIME multipart construction examples otherwise. Or decide on a PUT request, keep -InFile, but have php read the raw payload from `php://input` (plain upload data, no file name then). – mario Jan 28 '19 at 04:46
  • Yes - the multipart construction appeared to work fine. Cheers @mario. – Connor Varney Jan 28 '19 at 04:57

1 Answers1

0

Because of my Powershell version, what worked best was @mario's solution being a manual MIME construction as shown in example 2 of the v5.1 docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-5.1

Cheers.