/    Sign up×
Community /Pin to ProfileBookmark

directory search and display

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[/URL]

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;
}
[/code]

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>’;
}
}
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 05.2017 — I'm afraid I don't understand the problem you want to solve. ? In the second function, you refer at different places to a hard-coded "p1IH.txt" file, in other places "$file_names[1]", and yet in other places "$file_names[0]". Which file do you want to validate, and what is the criteria that makes it valid/invalid?
Copy linkTweet thisAlerts:
@marcellioauthorNov 06.2017 — In that dir_files function I have stored all files what are in the directory. In function module_header ignore that echo file name calling out file_names[0] because that is just temporary output.

I need now for each loop that will go through the $file_names and to display only one name of the file if is match with .txt and if is the correct file.

When I change the path of the file on the top to different file it must match the file and display the name of the file. Does that make a sense.
Copy linkTweet thisAlerts:
@NogDogNov 06.2017 — In that dir_files function I have stored all files what are in the directory. In function module_header ignore that echo file name calling out file_names[0] because that is just temporary output.

I need now for each loop that will go through the $file_names and to display only one name of the file if is match with .txt and if is the correct file.

When I change the path of the file on the top to different file it must match the file and display the name of the file. Does that make a sense.[/QUOTE]


Hmm...not entirely, but it sort of sounds like maybe you just need an in_array() call?
[code=php]
if(in_array(basename($file_name), $file_names)) {
// match found
}
else {
// no match found
}
[/code]

?
Copy linkTweet thisAlerts:
@marcellioauthorNov 06.2017 — Hmm...not entirely, but it sort of sounds like maybe you just need an in_array() call?
[code=php]
if(in_array(basename($file_name), $file_names)) {
// match found
}
else {
// no match found
}
[/code]

?[/QUOTE]


Yes I was thinking about that in_array and that function basename. That will match only txt files? so if I put .xml and any other file it will output no match?

Btw. you put in there new variable $file_name or did you just miss the S on the end.
Copy linkTweet thisAlerts:
@NogDogNov 06.2017 — The in_array() will just be a string comparison in this case, so if you need fuzzier logic, then you'll need to account for it, as basename() just strips off any directory portion so that it's only the file name. You could make a little function to strip off any suffix, I suppose. Then you'd need to apply it both to each of the array value, as well as the file name being tested.
[code=php]
function strip_suffix($file_name)
{
return preg_replace('/.[^.]+b/', '', $file_name);
}

if(in_array(strip_suffix($file_name), array_map('strip_suffix', $file_names))) {
// match found
}
[/code]

UNTESTED!
×

Success!

Help @marcellio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...