<?php // place this code inside a php file and call it f.e. "download.php" $path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure $fullPath = $path.$_GET['download_file']; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; // example: place this kind of link into the document where the file download is offered: // <a href="download.php?download_file=some_file.pdf">Download here</a> ?>
Students who are interested to try this problem can earn and extra points or exemption for finals. Solve the problem using PHP scripting language.
Show your codes on October 19, 2008 (9:00) to your instructor for checking and evaluation.
The problem was excerpt from HP CodeWars 2009.
Redundant Acronym Syndrome Syndrome
Task Description
From Wikipedia, the free encyclopedia:
The term RAS syndrome refers to the use of one of the words that make up an acronym as well as the abbreviation itself, thus in effect repeating that word. It stands for “Redundant Acronym Syndrome syndrome,” and is itself a humorous example of a redundant acronym.
Write a program that generates RAS syndrome acronyms.
Program Input
Each line of the input contains one or more words. The line of text will be no longer than 80 letters, spaces, and/or punctuation. The last line of the input is the word END.
automated teller machine
random access memory
alternating current
scholastic aptitude test
international standard book number
END
Program Output
The program must print the acronyms in RAS syndrome format. For this program you can assume that the repeated word is always the last word of the input line. Acronyms must be upper-case, regardless of the case of the input.
ATM machine
RAM memory
AC current
SAT test
ISBN number
Here is my example of creating a graph using an object – PHPLOT. PHPLOT is an easy-to-use object which is now available and freeing yourself from lengthy code. The only requirement is your knowledge in using a class, properties and methods. The object is available is around the corner.
*********************************/
require_once(“mysqlidb.class.php”);
require_once(“config.php”);
require_once(“phplot.php”);
/* initialize connection */
$report = new mysqlidb(DB_HOST,DB_USER,DB_PWORD,DB_DATABASE);
$report->dbConnect(); //specify connection
$year = (!isset($_POST['year']))?date(“Y”):$_POST['year'];
/* retrieve result from database */
$query = “SELECT monthna,revenue FROM listofmonthlyrev WHERE ryear = $year”;
$report->viewRecord($query);
/* create an array */
$array_rep = array();
$ctr = 0;
while ($row = $report->fetchArray()){
$array_rep[$ctr] = array($row[0],$row[1]);
$ctr++;
}
/* free result */
$report->freeResult();
/* instantiate and define graph object */
$graph = new PHPlot();
/* assign data to plot */
$graph->SetImageBorderType(‘plain’);
$graph->SetPlotType(‘bars’);
$graph->SetDataType(‘text-data’);
$graph->SetDataValues($array_rep);
/* Main plot title */
$graph->SetTitle(“REVENUE FOR THE YEAR $year”);
/* Make sure Y=0 is displayed */
$graph->SetPlotAreaWorld(NULL, 0);
/* Y Tick marks are off, but Y Tick Increment also controls the Y grid lines */
$graph->SetYTickIncrement(100);
/* Turn on Y data labels */
$graph->SetYDataLabelPos(‘plotin’);
/* Turn off X tick labels and ticks because they don’t apply here: */
$graph->SetXTickLabelPos(‘none’);
$graph->SetXTickPos(‘none’);
/* With Y data labels, we don’t need Y ticks or their labels, so turn them off */
$graph->SetYTickLabelPos(‘none’);
$graph->SetYTickPos(‘none’);
/* Format the Y Data Labels as numbers with 1 decimal place. */
/* Note that this automatically calls SetYLabelType(‘data’). */
$graph->SetPrecisionY(1);
/* draw the graph */
$graph->DrawGraph();
/* close connection */
$report->disConnect();

graph using phplot
The script below is a sample on how to create a bar graph using PHP language.
$arr_value = array(1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 50);
/* image size and margin */
$img_height = 400;
$img_width = 600;
$margins = 20;
/* size of the graph – subtracting the size of borders */
$graph_width = $img_width – $margins * 2;
$graph_height = $img_height – $margins * 2;
$imageObj = imagecreate($img_width,$img_height); //create the image
/* define the width of the bar */
$bar_width = 20;
$total_bars = count($arr_value); //number of bars
//in – between spaces of the bar graph
$gap = ($graph_width – $total_bars * $bar_width)/($total_bars + 1);
/* define the colors which will be used for bars, backgrounds, lines and borders */
//int imagecollorallocate(image, int red, int green, int blue)
$bar_color = imagecolorallocate($imageObj,0,64,128);
$background_color = imagecolorallocate($imageObj,240,240,255);
$border_color = imagecolorallocate($imageObj,200,200,200);
$line_color = imagecolorallocate($imageObj,220,220,220);
/* border within the perimeter of the graph */
//bool imagefilledrectangle(image, int x1, int y1, int x2, int y2, int color)
imagefilledrectangle($imageObj,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($imageObj,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
/* max height of the graph */
$max_value = max($arr_value);
$ratio = $graph_height/$max_value;
/* draw horizontal lines inside the graph area */
$horizontal_lines = 20;
$horizontal_gap = $graph_height/$horizontal_lines;
for($i=1; $i<=$horizontal_lines; $i++){
$y = $img_height – $margins – $horizontal_gap * $i;
//bool imageline(image, int x1, int y1, int x2, int y2, int color
imageline($imageObj,$margins,$y,$img_width-$margins,$y,$line_color);
$v = intval($horizontal_gap * $i/$ratio);
//bool imagestring(image, int font, int x, int y, int string, int color)
imagestring($imageObj,0,5,$y-5,$v,$bar_color);
}
/* Drawing the individual bar */
for($i=0; $i<$total_bars; $i++){
list($key,$value) = each($arr_value);
$x1 = $margins + $gap + $i * ($gap + $bar_width);
$x2 = $x1 + $bar_width;
$y1 = $margins + $graph_height – intval($value * $ratio);
$y2 = $img_height – $margins;
imagefilledrectangle($imageObj,$x1,$y1,$x2,$y2,$bar_color);
imagestring($imageObj,0,$x1 + 5,$y1 – 10, $value, $bar_color);
imagestring($imageObj,0,$x1 + 3,$img_height – 15,$key,$bar_color);
}
header(“Content-type:image/png”);
imagepng($imageObj);
Output of the above PHP script.

Bar Graph