3

I am attempting to create an agent to collect data and hopefully over HTTP post the file to a webserver running apache2. I am currently using the the following python script to hopefully push the file.

import requests
txt = {'file': open('/tmp/pysrv01-20151207-212735')}
post = requests.post('someaddr', files=txt)

I understand that possible a simple php page could accept the post and store the file in a directory. Any idea's to something simple and lightweight? Maybe this is a wrong approach.

1
  • 1
    POST itself is not particularly special when talking about cross-language. In order to accept a POST request in PHP you should look at $_POST the PHP itself should not care where the POST request came from, but may have difficulty parsing the string it receives. Commented Dec 8, 2015 at 3:46

1 Answer 1

4

PHP has documentation for handling file uploads. Start reading here http://php.net/manual/en/features.file-upload.php

You use the $_FILE variable to detect the files included in Python's POST request. Here's an example given in the PHP doc ...

<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing me in the right direction. It was failing however for some key value in $_FILES. Did some more research as managed to get this to work. <?php $name = $_FILES['file']['name']; $temp_name = $_FILES['file']['tmp_name']; if(isset($name)){ if(!empty($name)){ $location = '/var/www/html/uploads/'; if(move_uploaded_file($temp_name, $location.$name)){ echo 'uploaded'; } } } else { echo 'please uploaded'; } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.