this is my website page of diplaying data from file. [URL=”http://titan.dcs.bbk.ac.uk/~mzacha02/p1tma/index.php”]http://titan.dcs.bbk.ac.uk/~mzacha02/p1tma/index.php
I need to know how do I change the file name if the file is different and it will display on the first line the correct file name. I have tried to use foreach loop because all files I have stored in the array. But then I don’t know how to match the file that is open inside of that array that will display the correct name of the file.
thanks regards marcellio
this is function that return array of files
[code=php]
function Dir_file(){
$file_names = array();
$new_file = opendir(‘data’);
while(false !== ($file = readdir($new_file))){
#below is required to ignore the . and .. N.B. you need to give full path
#week 2 slide 8 to revise array sysntax
if (is_file(‘data/’ . $file)) {
$file_names[] = pathinfo($file, PATHINFO_BASENAME); #use this to get only the file name with no extension
}
}
return $file_names;
}
this is the first box that shows on the page file name; module, tutor and date.
[code=php]
function Module_Header($file_names){
//Checking if the file exist
if (file_exists(‘data/p1IH.txt’)) {
//If exists open file
$handle = fopen(‘data/p1IH.txt’,’r’);
if ($handle) {
$name = fgets($handle, 1024);
$details = explode(‘,’, $name);
echo ‘<p>File name: ‘ . $file_names[1] . ‘</p>’;
//Initialize variables and store our validation then check if is correct
$a = $details[0];
$b = $details[3];
$module = ValidModuleCode($a);
$date = ValidDate($b);
if ( $module == false ){
echo ‘<p>Module Code : ‘ . $details[0] . ‘ : ERROR’ . ‘</p>’;
} else{
echo ‘<p>Module Code : ‘ . $details[0] . ‘</p>’;
}
if ( ctype_alpha($details[1]) ){
echo ‘<p>Module Title : ‘ . $details[1] .'</p>’;
} else{
echo ‘<p>Module Title : ‘ . $details[1] . ‘ERROR’ . ‘</p>’;
}
if ( ctype_alpha($details[2]) ){
echo ‘<p>Tutor : ‘ . $details[2] .'</p>’;
} else{
echo ‘<p>Tutor : ‘ . $details[2] . ‘ERROR’ . ‘</p>’;
}
if ( $date == false){
echo ‘<p>Marked date : ‘ . $details[3] . ‘ERROR’ . ‘</p>’;
} else{
echo ‘<p>Marked date : ‘ . $details[3] . ‘</p>’;
}
}
else {
echo ‘<p>’ . $file_names[0] . ‘.txt – error openning file!</p>’;
}
fclose($handle);
}
else {
echo ‘<p>’ . $file_names[0] . ‘.txt – file not found!</p>’;
}
}