【问题标题】:file_get_contents cuts first 9 lines of the filefile_get_contents 剪切文件的前 9 行
【发布时间】:2013-07-23 15:22:53
【问题描述】:

我想在不执行文件的情况下包含一个代码示例。
但我不知道为什么 fread() 或 file_get_contents() 这样的函数会剪切包含文件的前 9 行。

包含而不执行代码(我尝试过的多种方法之一):

<?php
// get contents of a file into a string
$filename = "index_GD.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo "<pre><codesample>".$contents."</codesample></pre>";
fclose($handle);
?>


包含文件的前 11 行:

<?php

// Set the content-type
header('Content-Type: image/png');

function imagegradientellipse($image, $cx, $cy, $w, $h, $ic, $oc){
 $w = abs($w);
 $h = abs($h);
 $oc = array(0xFF & ($oc >> 0x10), 0xFF & ($oc >> 0x8), 0xFF & $oc);
 $ic = array(0xFF & ($ic >> 0x10), 0xFF & ($ic >> 0x8), 0xFF & $ic);
 $c0 = ($oc[0] - $ic[0]) / $w;


输出(前 11 行):

 > 0x10), 0xFF & ($oc >> 0x8), 0xFF & $oc);
 $ic = array(0xFF & ($ic >> 0x10), 0xFF & ($ic >> 0x8), 0xFF & $ic);
 $c0 = ($oc[0] - $ic[0]) / $w;
 $c1 = ($oc[1] - $ic[1]) / $w;
 $c2 = ($oc[2] - $ic[2]) / $w;
 $i = 0;
 $j = 0;
 $is = ($w<$h)?($w/$h):1;
 $js = ($h<$w)?($h/$w):1;
 while(1){

【问题讨论】:

  • 你可以简单地使用php file('path to file')函数来获取文件的内容而不用解析它。

标签: php include


【解决方案1】:

使用 htmlspecialchars http://php.net/manual/en/function.htmlspecialchars.php

echo htmlspecialchars($contents);

如果您检查页面来源,您可能会在其中找到所有文本,尽管浏览器不会显示它。

有一个类似的问题: php: file_get_contents is stripping out php code

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多