Sometimes I need to get a copy of a file from one of the machines in the office. I can use scp to copy from a Linux machine, but for Windows PCs my remote access software doesn't allow me to do file transfers without first taking control of the users PC. I can however run commands on the Windows command prompt. New versions of Windows 10 come with SSH installed so it is possible to remotely run scp and get it to copy a file to my server, but that would require first setting up SSH and importing the authentication keys on the Windows PC. I found it much simpler to just create a little PHP script on my web server and then use curl to send it from the Windows command prompt. The curl command has been included in Windows for even longer than SSH, and doesn't need any setting up before use

My PHP script, which I have called curlup.php looks like this:

<?php
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES['fileup']['name']);
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $target_file))
{
    echo("File " . htmlspecialchars(basename($_FILES['fileup']['name'])) . " uploaded");
}
else
{
    echo("Error");
}
?>

It's very simple and doesn't do any checking, so be careful what you do with it. You will also need a directory called uploads in the same directory as the script, and this needs to be writable and executable by your web server user which could be www-data, or apache.

To upload a file you just need the line: curl -F "fileup=@mylog.txt" https://webserver.org/secretstuff/curlup.php

This is the same command for both Linux and Windows (and I guess Mac OS X, but I haven't tried it). One thing I used it for recently was to grab a sleep study report from a Windows PC to find out if it had been sleeping and waking when it was meant to. That can be done with the 2 commands:

powercfg /sleepstudy
curl -F "fileup=@C:\WINDOWS\system32\sleepstudy-report.html" https://webserver.org/secretstuff/curlup.php

Previous Post Next Post