You could try something like the following perhaps:
var request = require('request');
var fs = require('fs');
var data = {
file: fs.createReadStream( '/path/to/my/image.jpg' )
};
request.post({ url:'http://example.com/upload.php', formData:data }, function callback( err, response, body ) {
if( err ) {
return console.error( 'Failed to upload:', err );
}
console.log( 'Upload successful!' );
});
Or, to create a page that allows the user to select the photo to upload
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="http://example.com/upload.php" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="usrfile" />');
res.write('<input type="submit" />');
res.write('</form>');
return res.end();
}).listen(8088);
On the PHP server to handle the upload you could do this:
<?php
/* node.js upload target ~ "upload.php" */
if( isset( $_FILES ) ){
/* change path to suit environment */
$dir='c:/temp/fileuploads/1/';
$obj=(object)$_FILES['file'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$result = move_uploaded_file( $tmp, $dir.$name );
echo $result ? 'File '.$name.' ws moved to '.$dir : 'Error: Failed to save '.$name;
}
?>