PHP获取文件输入文本


PHP get file with input text

我有一个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>