0

Can someone explain me why when i POST RAW Data for example "test.txt" in the below script

<?php

echo file_get_contents("php://input");

?>

it only prints the text "test.txt" instead of the file contents of that file?

Thank you

2
  • Do you want the content of a local file, of the content from a client file? Commented May 30, 2014 at 11:05
  • It isn't a file, it's a stream containing your raw POST data. What's the problem? It did what you asked it to? Are you trying the highly insecure echo file_get_contents(file_get_contents("php://input"));? Commented May 30, 2014 at 11:07

2 Answers 2

1

Your code reads the contents of the raw POST data and echoes it back.

Whereas what you want is this:

// retrieve the requested filename
$fileName = file_get_contents("php://input");

// echo the contents of the requested file
echo file_get_contents($fileName);

Depending on what you're trying to, you may wish to sanitize the $fileName input (not shown: too broad) and restrict access to a specific local directory:

$path = $myLocalDirectory . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($path) {
    echo file_get_conents($path);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Note that this has major security issues, because everybody could read data from any file
@SanderVisser duly amended. It answers the question: anything further is dependent on what the OP is trying to achieve.
Yes i know that it has security issue. No worries :). Thanks for your answer
@bcmcfc A bit late, but please note that your sanitation doesn't limit you to that directory (../../index.php). ltrim($fileName, './') would work wonders, but wouldn't work with hidden files (.htaccess).
@h2ooooooo well, yeah - there is no sanitation of the fileName, as mentioned in the answer.
|
0

Try like this ..

$input = "abc.txt";
echo file_get_contents($input);

It gives the content of the text file abc.txt

Comments

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.