user3393046
user3393046

Reputation: 163

PHP Input & file_get_contents

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

Upvotes: 0

Views: 5047

Answers (2)

bcmcfc
bcmcfc

Reputation: 26825

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);
}

Upvotes: 1

Deepu Sasidharan
Deepu Sasidharan

Reputation: 5309

Try like this ..

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

It gives the content of the text file abc.txt

Upvotes: 0

Related Questions