PHP获取带有输入文本的文件

PHP获取带有输入文本的文件,php,file-get-contents,Php,File Get Contents,我有一个php页面: <?php $stats = file_get_contents('http://localhost/test/stats/$name.txt'); $name = $_POST['stat']; echo "$stats"; echo "$name"; ?> <form action="index.php" method="POST"> <input type="text" name="stat" /> <i

我有一个php页面:

<?php


$stats = file_get_contents('http://localhost/test/stats/$name.txt');
$name = $_POST['stat'];
echo "$stats";
echo "$name";

?>
<form action="index.php" method="POST">
    <input type="text" name="stat" />
    <input type="submit" value="Upload" />
</form>

我想从文本表单中获取一个自定义文件,只需输入文件名,但我的代码不起作用。谢谢


<?php
if(isset($_POST['stat'])) {
  $name = $_POST['stat'];
  // note the double quote here
  $stats = file_get_contents("http://localhost/test/stats/$name.txt");
  echo "$stats";
  echo "$name";
}
?>
<form action="index.php" method="POST">
    <input type="text" name="stat" />
    <input type="submit" value="Upload" />
</form>

别忘了保护您的文件(不包括“./”)

是的,此代码将不起作用。请尝试以下代码:

<?php
    $name = $_POST['stat'];

    $stats = file_get_contents("http://localhost/test/stats/$name.txt");

你犯了两个错误

首先,在需要文件名的代码后面加上文件名

第二:php当您将一个php变量名放在单引号“'''”中时,就像在您的例子中一样,它将按原样呈现,而其值不会呈现。但如果将php变量置于双引号中,则会呈现/显示其值

现在试试这个,如果你还有任何问题,请告诉我

谢谢


<?php

// switch the order around
$name = $_POST['stat'];
// and remove the variable from a single quoted string.
$stats = file_get_contents('http://localhost/test/stats/'.$name.txt);
echo "$stats";
echo "$name";

?>
<form action="index.php" method="POST">
    <input type="text" name="stat" />
    <input type="submit" value="Upload" />
</form>

你认为这可能与
$name
$stats
中使用后如何给它赋值有关吗?
$stats
$name
是互换的。第一个
$name=$\u POST['stat']然后
$stats=file\u get\u contents(“http://localhost/test/stats/$name.txt”)+1用于提及安全性。