HELP ME!! simple program (i think) not working...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Spydamonky
Forum Newbie
Posts: 2
Joined: Tue Nov 02, 2010 10:52 am

HELP ME!! simple program (i think) not working...

Post by Spydamonky »

Hey guys i was asked by a friend to write a piece of code to read a .csv file and then using the google charts API make a graph. The program worked perfectly until I added a if(file_exsits('data.csv') copy ('data.csv', /current/data.csv); it wont copy and because of this the whole program does not run. Its ment to run as a img on a html page. any ideas...

Code: Select all

<?php
  header('content-type: image/png');
  $url = 'http://chart.apis.google.com/chart?chid=' . md5(uniqid(rand(), true));
  $max = '';
  $scale = '';
  //Crack Height Below
  $crack = '50';
  if(file_exists('data.csv')){
  copy('data.csv', '\current\data.csv');}
  $file = fopen('\current\data.csv', 'r');
while (!feof($file) )
{
$line = fgetcsv($file, 40, ',');
$time .= $line[0].'|';
$date = $line[1];
$chd .= $line[2].',';
$scale .= $line[2].'|';
$crackH .= $crack.',';
if ($line[2] > $max)
$max = $line[2];
}
  $chd = substr($chd, 0, -1);
  $scale = substr($scale, 0, -1);
  $time = substr($time, 0, -1);
  $crackH = substr($crackH, 0, -1);
  // Add data, chart type, chart size, and scale to params.
  $chart = array(
    'cht' => 'lc',
    'chs' => '1000x300',
	'chd' => 't:'.$chd.'|'.$crackH,
	'chds' => '0,'.$max,
	'chco' => '00FF00,FF0000',
	'chxt' => 'x,x,y,y,t,r',
	'chxl' => '0:|'.$time.'|1:|Time|3:|Levels|4:|'.$time,
	'chxr' => '2,0,'.$max.',50|5,0,'.$max.',50',
	'chxp' => '1,50|3,50',
	'chdl' => 'Levels|Crack Height',
	//Client Name Below
	'chtt' => 'CLIENT NAME |'.$date,
    'chts' => '0000FF,20');
  $context = stream_context_create(
    array('http' => array(
      'method' => 'POST',
      'content' => http_build_query($chart))));
  fpassthru(fopen($url, 'r', false, $context));
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: HELP ME!! simple program (i think) not working...

Post by Jonah Bron »

If this is on a server, you probably don't have access to /current/. Try changing copy('data.csv', '\current\data.csv'); to

Code: Select all

copy('data.csv', dirname(__FILE__) . '/current/data.csv');
Spydamonky
Forum Newbie
Posts: 2
Joined: Tue Nov 02, 2010 10:52 am

Re: HELP ME!! simple program (i think) not working...

Post by Spydamonky »

thank you! ill test it now is the (_FILE_) a string?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: HELP ME!! simple program (i think) not working...

Post by Jonah Bron »

Yes. __FILE__ is a magic constant containing the current file name.
Post Reply