receive in a php(server) a POST send by java (client) - php

i want to receive in a server a xml file sent by java by POST
how can e receive that ? and how can e parse the file, in this case a xml file...
regards.

There are numerous tutorials on handling file uploads (including: http://www.php.net/manual/en/features.file-upload.post-method.php ) if you're actually uploading a file. If, instead, you're posting the contents of the file, the contents will be in the $_POST and $_REQUEST arrays - PHP creates and populates these for you.
This is a broad (and basic) question, so a more specific answer will require you to read some tutorials, try some code, and post specific questions about problems you encounter.

If it's a file upload (enctype="multipart/form-data"), use the $_FILES superglobal: Handling File Uploads.
Otherwise, just refer to it by its form field name: $_POST['xmlfile'].
Either way, parse it using the DOM or SimpleXML.

if it's XMLRPC request, you have to use either $HTTP_RAW_POST_DATA or php://input wrapper.

Related

Python to PHP on server send image

I have a raspberry pi running a lamp stack, an arduino and a camera hooked up. The end goal is that when my arduino takes a photo, it then writes an image to a php address which is then emailed.
Right now, I'm trying to get the image to get placed in the right place.
Here's my php snippet:
<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/images/mypic.jpg");
?>
My python code is doing:
import requests
r = requests.get('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
r2 = requests.post('http://192.168.1.100/accept_image.php', data = r.content)
I realize the image is going to get overwritten. That's not a problem. I can always add a timestamp later etc etc.
However, this gives me an error code. I'm a beginner at php and use python mainly for scientific computing so not sure if I'm passing the picture correctly. I know that the ip is correct as I can connect to it and it's all in network.
I have looked at this Python script send image to PHP but am still getting stuck.
EDIT:
Upon further debugging:
print_r($_POST);
returns an empty array. Not sure why?
To have a file accessible to PHP in $_FILES, you must use HTML-form style encoding of files (multipart/form-data). This is different from a standard POST request including the content in the request body. This method is explained in http://php.net/manual/en/features.file-upload.post-method.php - the key here is:
PHP is capable of receiving file uploads from any RFC-1867 compliant browser.
What's you're trying to do is not sending it the RFC-1867 way, but a plain-old POST request.
So you have two options:
Send the data from Python using multipart/form-data encoding. It shouldn't be too hard but requires some work on the Python side.
Just grab the data on the PHP side not from $_FILES but by directly reading it from the POST body, like so:
.
$data = file_get_contents('php://input');
file_put_contents("/var/www/images/mypic.jpg", $data);
It's more of a memory hog on the PHP side, and means you need to do some validation that you actually got the data, but is quite simpler.
To clarify, in PHP $_POST is only populated when the Content-type request header is multipart/form-data or application/x-www-form-urlencoded (and of course the data is encoded in the proper way).
If you get a POST request with anything else in the body, you can read it directly by reading from the php://input stream, and you're responsible for handling / decoding / validating it.

jQuery load php file as text?

Is it possible to load a php file as text with jquery?
$('#loader').load('somefile.php', function(e){
console.log(e);
});
This always interprets/execute the php file but I'm looking for a way to only load it as text, without to resort to renaming my php file as .txt
Is it possible?
Cheers
It is not possible without making any server side modification. The web server will always interpret the php file and return the output. However does not matter what solution you find it'll be very dangereous since you'll be dumping content of your php file to public.
Possible solutions with server side modifications:
Create a PHP file that dumps the content of a file, which name is specified by a url argument
Rename the file (I know the op does not want this, just included since it's an option)
As #nicholas-young suggested, get rid of the PHP tags.
I'm not sure why you need this type of need but I want to emphasize that this might not be a good idea in most of the cases since you'll be make a working PHP file available to public. If you can explain more why you need this we might offer better solutions.
Update:
Create a dumper.php that requires authorization and call this file from the javascript side with passing the filename that you want to be dumped as a parameter (dumper.php?file=index.php)
echo file_get_contents($_GET['file']);
It is of course not possibile.
.load will make an HTTP request to yourwebsite.com/somefile.php hence you will obtain the result of your script not the PHP code inside it.
If you really need the raw code inside your javascript as a string you should output it from the php itself:
<script>
var yourCode = <?=json_encode(file_get_contents('somefile.php')) ?>;
</script>
NO! Would be a major security problem if possible. The header will not matter. If making request towards php file, it will execute prior to delivery.
Use some parameter to print out contents from file instead. But do it in the file itself.

How to get the name of a file sent to a php://input via wget post-file

I wrote a script that receives an image file through file_get_contents('php://input') and does some other magic. The file is sent from a client using the wget post-file=blahblah.blah command and it's all working fine.
The issue that I have is that I need to use the name of the received image file as a string for processing purposes and file_get_contents() gives me the content of the file but not the name of it.
Would anyone know how I can get the name?
Any answer that can put me in the right direction would be well appreciated.
You can't. The --post-file option to wget sends the contents of the file as a raw POST request; it does not treat it as a file upload, so the name of the file is not transmitted. Per the wget manual page:
--post-data=string
--post-file=file
Use POST as the method for all HTTP requests and send the specified
data in the request body. --post-data sends string as data,
whereas --post-file sends the contents of file. Other than that,
they work in exactly the same way. In particular, they both expect
content of the form "key1=value1&key2=value2", with percent-
encoding for special characters; the only difference is that one
expects its content as a command-line parameter and the other
accepts its content from a file. In particular, --post-file is not
for transmitting files as form attachments: those must appear as
"key=value" data (with appropriate percent-coding) just like
everything else.
There are numerous ways to go about getting the file name. I believe the classic way would be using url parameters ( POST your file to example.com/your-script.php?name=some-file-name-here ) but an alternative and possibly cleaner way would be using custom http headers: 'X-Filename: your-file-name'.
wget --header "X-Filename: your-file-name" --post-file /your/file
Then in PHP check for the headers ( using apache_request_headers() for example ).
PHP has a built in superglobal variable $_FILES, it works similar to $_POST.
$_FILES['/*the html object name goes here*/']['name']
The ['name'] part of the array returns the actual name of the file uploaded from the html object name.
So to clarify, if your html was <input type="file" name="screenshot" />
your PHP would be $name = $_FILE['screenshot']['name']
$name now stores the string that holds the name of the file.

Save binary request body as file

I wrote a program that reads a binary file into the RAM and then sends it using an HTTP request to my server. It uses the PUT method and the binary file is (in) the body.
Now how can I tell my server to receive and safe the file in a folder?
If possible without any additional libraries that I would need to download (unless it's more efficient).
I know, there are some similar threads to this one, but they either they where about receiving text or they were about doing it with libraries or there simply was no sufficient answer.
I'd also like to know, if it would be more efficient or smarter to use the POST method or any other instead of PUT.
You can get at the data by opening a stream to php://input, like so:
$datastr = fopen('php://input',rb);
if ($fp = fopen('outputfile.bin', "wb")){
while(!feof($datastr)){
fwrite($fp,fread($datastr,4096)) ;
}
}
As to whether to use POST or anything else depends on what is happening with the data, and whether you care about being RESTful or such. See other questions/answers, indempotency.
The advantage I would see with using POST is that it's more commonly used (on most submission forms where you upload a file), and therefore has more support from within PHP and html.

Using cURL to upload a file without # symbol?

I have a slight problem here, how can I tell cURL specifically to attach a file to a request?
If I am uploading a file with cURL, then the common method is to attach it as part of POST data array with the value having # in front of it, for example:
CURLOPT_POSTFIELDS=>array('my-file'=>'#my-file.txt')
This would obviously work, but I have two problems with it:
What if it is not actually a file I am uploading? What if my POST value actually IS '#my-file.txt' and it attempts to upload the file instead? It creates a loophole I am desperately trying to avoid.
How can I upload a file from a URL? Would I have to download it, store it in temporary folder and then attach it with # from that temporary folder? Why can't I give cURL just contents that I wish to use as file?
cURL CURLOPT_INFILE is not an option, since it won't show up as part of $_FILES array.
It seems like such a loophole in cURL to be dependent on # symbol in the POST field value. Is there a way around it? Why isn't there a CURLOPT_FILEFIELDS array? Command-line cURL has a separate flag for this (-F), but I don't see it as an option in PHP for some reason.
Any help would be greatly appreciated, thanks!
In case anyone is viewing this in 2019 and beyond after finding this result on Google (as I have) the issue has been resolved as per this bug fix.
You can now use CURLFile to send files via PHP CURL requests as below:
$fullpath = '/var/www/html/website/test.png';
$mime = 'image/png';
$publicname = 'testpic';
$cfile = curl_file_create($fullpath,$mime,$publicname);
$fields[$publicname] = curl_file_create($fullpath,$mime,$publicname);
Which would be then used in e.g.
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
Further information can be found here.
What I ended up doing is detecting if any of the input data started with an # symbol and if they did, then submitting them as a GET variable (as part of the submit URL in cURL). It does not help with all cases (such as when a large string is submitted that starts with an #), but cuts out the problems I had with Twitter handles.

Categories