Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents('php://input') returning empty string with a PUT request

After moving one of our websites from Linux with Apache to Windows with IIS (8.5) running PHP 5.6 via FastCGI, we've run into the problem that file_get_contents('php://input') returns an empty string for PUT requests.

I've created the following test:

<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
    strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    die(file_get_contents('php://input'));
}
?>
<!DOCTYPE html>
<html>
<head>
    <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
</head>
<body>
    <h2>POST:</h2>
    <div id="post"></div>

    <h2>PUT:</h2>
    <div id="put"></div>
    <script>
        $.ajax({
            url: '?',
            data: 'Working',
            type: 'POST'
        }).then(function(response) {
            $('#post').html(response || 'Not working');
        });

        $.ajax({
            url: '?',
            data: 'Working',
            type: 'PUT'
        }).then(function(response) {
            $('#put').html(response || 'Not working');
        });
    </script>
</body>
</html>

Which results in:

POST:

Working

PUT:

Not working

What could be causing this?

like image 724
sroes Avatar asked Feb 19 '15 16:02

sroes


People also ask

What does file_get_contents PHP input do?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Does file_get_contents cache?

Short answer: No. file_get_contents is basically just a shortcut for fopen, fread, fclose etc - so I imagine opening a file pointer and freading it isn't cached.


1 Answers

As it turns out the problem is caused by the Helicon Ape module (a module for supporting Apache .htaccess and .htpasswd configuration files). Removing this module fixes the problem, but I still don't know why it would interfere with PUT requests. I guess I'll have to post a topic on their forum with this issue.

like image 161
sroes Avatar answered Nov 16 '22 02:11

sroes