The user uploads a plain text file with a decimal value(eg. 36.81) on each line of the file. I have to upload this, open it and make a histogram out of it. I have done everything but cannot do it with the help of the img tag which I am supposed to do. Here is the code I have written as of yet:
Code for uploading the file and feeding all values in an array
Code to create a histogram using the $data array(histogram.php)
At present, it works because both the pieces of codes are in one php file. But I need to have them in this way and they don't work because the data array cannot be read by histogram.php. Anyone knows a way to do that?
Code for uploading the file and feeding all values in an array
<?php
if(($_FILES['img'] != '') && ($_FILES["img"]["type"] == "text/plain"))
{
move_uploaded_file($_FILES['img']['tmp_name'], "upload/" . $_FILES['img']['name']);
}
else
{
die ("There is an error in your upload. <br> Either the file is not a .txt file or you havent selected a file") ;
}
$data = array();
$fp = @fopen ("upload/" . $_FILES['img']['name'], "r");
if ($fp)
{
while(!feof($fp))
{
$this_line = fgets($fp);
$this_line = chop($this_line);
array_push($data,$this_line);
}
fclose($fp);
}
echo("<img src='histogram.php?' alt='None' />");
?>
Code to create a histogram using the $data array(histogram.php)
$sum = array_sum($data);
$num = count($data);
header("Content-type: image/jpeg");
$height = 700;
$width = 50*$num;
$im = imagecreate($width,$height);
$white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,220,0,0);
imageline($im, 10, 5, 10, 600, $black);
imageline($im, 10, 600, 200*$num, 600, $black);
// Now plot each column
$x = 15;
$y = 600;
$x_width = 20;
$y_ht = 0;
for ($i=0;$i<$num;$i++){
$y_ht = $data[$i];//$sum)* $height;
imagefilledrectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-2,$y+10,$data[$i],$black);
$x += ($x_width+30);
}
imagejpeg($im);
imagedestroy($im);
At present, it works because both the pieces of codes are in one php file. But I need to have them in this way and they don't work because the data array cannot be read by histogram.php. Anyone knows a way to do that?