jpGraph can't include PHP file

邮差的信 提交于 2019-12-12 01:09:52

问题


I am trying to include one PHP file in another PHP file that creates a jpGraph image

(The reason is that I am loading mySQL data for the chart, and I want to put the login credentials into a separate file)

I know that the chart is created (because a correct image file is created) but the chart does not show up in the web page.

Here is a simplified code example:

login.inc.php

  <?php
  $lhostname="localhost";
  $lusername="joeschmack";
  $lpassword="autumnleaf";
  $ldatabase="customers";
  ?> 

accbarex1.html

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title></title>
  </head>
  <body>
    <h3>This is where I want to display my graph</h3>
    <img src="accbarex1.php">
  </body>
</html>

accbarex1.php

<?php // content="text/plain; charset=utf-8"

require_once ('../../../lib/jpgraph/jpgraph.php');
require_once ('../../../lib/jpgraph/jpgraph_bar.php');
include("./login.inc.php");


$data1y=array(-8,8,9,3,5,6);
$data2y=array(18,2,1,7,5,4);

// Create the graph. These two calls are always required
$graph = new Graph(500,400); 
$graph->SetScale("textlin");

$graph->SetShadow();
$graph->img->SetMargin(40,30,20,40);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b1plot->SetFillColor("orange");
$b1plot->value->Show();
$b2plot = new BarPlot($data2y);
$b2plot->SetFillColor("blue");
$b2plot->value->Show();

// Create the grouped bar plot
$gbplot = new AccBarPlot(array($b1plot,$b2plot));

// ...and add it to the graPH
$graph->Add($gbplot);

$graph->title->Set("Accumulated bar plots");
$graph->xaxis->title->Set("X-title");
$graph->yaxis->title->Set("Y-title");

//$graph->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

// Display the graph
$graph->Stroke();

//save to file
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);

?>

The file shows the correct chart, but the web page accbarex1.html shows a broken image. If I comment out the line

include("./login.inc.php");

then both work.

Why? And how can I include a file in this situation?

Edit 5-13-2013:

Given that the include line is active. This helps (both file and embedded chart work)

  • ./login.inc.php does not exist
  • ./login.inc.php is empty

This does not help (only file works, embedded chart does not work)

  • use absolute path for include file
  • ./login.inc.php contains the line bla // PHP error
  • ./login.inc.php contains the line $i=1; // no PHP error, no PHP tags

Edit 5-14-2013:

Some slight progress: Firefox shows the same behavior, but at least I get an error message. Error console says:

Image corrupt or truncated: http:// .. acbarex1.php


回答1:


I found what the problem was. I had extra characters after the closing ?>, but I could not see them because they were spaces and newline. These characters messed up the jpGraph image.

Before:

  <?php
  $lhostname="localhost";
  $lusername="joeschmack";
  $lpassword="autumnleaf";
  $ldatabase="customers";
  ?><SPACE><SPACE><NEWLINE>

After:

  <?php
  $lhostname="localhost";
  $lusername="joeschmack";
  $lpassword="autumnleaf";
  $ldatabase="customers";
  ?>

That fixed it! Beware the extra characters!



来源:https://stackoverflow.com/questions/16530582/jpgraph-cant-include-php-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!