【问题标题】:using file_get_contents twice with a variable对变量使用 file_get_contents 两次
【发布时间】:2010-12-26 22:31:16
【问题描述】:

我正在尝试使用这段代码首先检索存储在我服务器上的 txt 文件中的 URL 并将其保存为变量,然后使用我刚刚检索并保存为的 URL 第二次运行 file_get_contents多变的。

代码适用于第一个 file_get_contents 并回显存储的 URL,但无法在第二个 file_get_contents 中使用该 URL 来回显 URL 的内容。

<?php
$files = file_get_contents('http://example.com/txtfile.txt');
echo $files;
$file = file_get_contents($files);
echo $file;
?>

【问题讨论】:

  • 回显 $files 会得到什么结果?
  • txtfile.txt 中有什么内容?
  • 可能包含一些空格,或者 URL 需要进行 urlencoded。试试 file_get_contents(trim($files));
  • $files 回显给了我存储在 txt 文件中的 URL。
  • @Andrew 为什么不回答这个问题。

标签: php file-get-contents


【解决方案1】:

那么解决您问题的直接方法是:

<?php
    $files = file_get_contents('http://example.com/txtfile.txt');
    echo $files;

    $files = trim($files);

    $file = file_get_contents($files);
    echo $file;
?>

但这是巨大的安全风险。运行 file_get_contents 来打开变量文件是有风险的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2014-02-06
    相关资源
    最近更新 更多