Jump to content

[SOLVED] Hiding files within a directory


crimsonjade

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.