Jump to content

Recommended Posts

Hi, I'm so sorry if this may have been asked before...but I've been searching and can't seem to find the right topic.

 

I'm looking for a PHP code that will hide (not unset) any filename within a directory that doesn't end in a particular extension (i.e. ".txt") from showing up in an array using foreach.

 

In this case, I am only showing txt files in an array displayed in an HTML selection form element.

 

I'm a total newbie at PHP (taking my first PHP course ever) and am entirely stuck on this.  Any help would be most appreciated!

 

Thanks in advance!

 

 

P.S. If this has been posted before (and I have a feeling it has) maybe a link or a keyword hint would suffice.  Thanks again ^^

~**Crimsonjade**~

Link to comment
https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/
Share on other sites

Ok basically I dont know a solution. I can however theorize one:

 


foreach(){
      $end = substr('$name', -3, 1);
      if($end != "txt"){
          $i++
      } else {
          echo "$name";
          $i++;
      }
}

 

Or something along those lines.

Actually, I ended up having to do this (as I had tried your suggestion...but then my select box came out blank...):

 

foreach($fileslist as $filenames) {


	list($name, $extension) = explode('.txt', $filenames);


	if (is_dir ($directory)){
		//I'll think of something to put here later...
	}
	else {
		die ("Directory 'courses' or required files do not exist.");
	}



	if (is_dir($filenames)) {
		continue;
	}

	elseif (!ereg("^.*\.(txt)$", $filenames)){
		continue;
	}

	echo "<option value=\"$filenames\">$name</option><br />";



}

 

But thanks anyway ^_^

Use the glob() function - it'll do exactly what you want in one line of code.

 

My goodness...that's...that's so much simpler...*frowns* unfortunately I'm only supposed to use that which we've learned about in class since this is what the assignment's testing *sigh*  But thank you!  Definitely will keep that in mind in the future!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.