-1

私は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>

ファイルの名前を入力するだけで、テキスト フォームからカスタム ファイルを取得したいのですが、コードが機能しません。ありがとう!

4

3 に答える 3

1
<?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>

あなたのファイルを保護することを忘れないでください(「../」を除く)

于 2013-06-15T17:00:37.460 に答える
0

はい、このコードは機能しません。以下のコードを試してください:

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

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

?>

あなたは2つの間違いを犯しています。

まず、ファイル名が必要なコードの後に​​ファイルの名前を付けています。

2番目:php変数名を単一引用符「 ''」で囲むと、あなたの場合のようにそのままレンダリングされ、その値はレンダリングされません。ただし、php 変数を二重引用符で囲むと、その値がレンダリング/表示されます。

今すぐこれを試してみて、まだ質問がある場合はお知らせください。

ありがとうございました

于 2013-06-15T17:00:54.527 に答える
0
<?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>
于 2013-06-15T17:03:54.837 に答える