ztealmax Posted June 21, 2007 Share Posted June 21, 2007 Ok what i need this to do is that it gets a list of all files in a directory, however i want it to show only files with an ending of for example .zip <?php $this = $PHP_SELF; $dir = $DOCUMENT_ROOT."files/"; $files = opendir($dir); $file_list = array(); $file_sort = array(); if(empty($sort))$sort="date"; if(empty($r)) $r=1; $cnt = 1; while ($file = readdir($files)) { $full_path = $dir."/".$file; if(!is_dir($full_path)) { $ext = explode("*.txt",$file); $i=count($ext); if($i>1)$file_details["ext"] = strtolower($ext[$i-1]); $file_details["name"] = $ext[0]; $file_details["date"] = filectime($full_path); $file_list[$cnt] = $file_details; $key = strtolower($file_details[$sort]); $file_sort[$cnt] = $key; $cnt++; } } if($r)arsort($file_sort); else asort($file_sort); ?> <table width="465" border="0" cellpadding="3" cellspacing="0" class="smallblacktext"> <tr> <td width="174"><?php print($this);?><?php print(!$r);?><? echo lan_11 ?></td> <td width="141"><?php print($this);?><?php print(!$r);?><? echo lan_12 ?></td> </tr> <tr bgcolor="#0033CC"> <td height="3" colspan="5"> </td> </tr> <?php while(list($key, $value)=each($file_sort)) { $value = $file_list[$key]; ?> <tr> <td width="174"><img src='<? echo BULLET ?>' /> <a href="<?=$_SERVER['PHP_SELF']?>?content=read2&text=files/<?php print($value["name"].".txt");?>"><?php print($value["name"]);?></a> <td width="141"><?php print(date("Y/m/d H:i",$value["date"]));?></td> </tr> <?php } ?> <? @include_once("$themes/$themedata/testa.php"); $caption="gurka"; require("$themes/$themedata/testa.php"); caption(); ?> <? echo $news; ?> </table> How would i go about that? //CHeers Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/ Share on other sites More sharing options...
trq Posted June 21, 2007 Share Posted June 21, 2007 Something like? <?php $dir = "/dir/to/look/at"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $ext = explode('.',$filename); if ($ext(count($ext)-1) = 'zip') { echo $filename."\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279266 Share on other sites More sharing options...
ztealmax Posted June 21, 2007 Author Share Posted June 21, 2007 I dont know what im doing wrong with the code you posted, but it doesnt dir anything at all //Cheers Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279275 Share on other sites More sharing options...
kenrbnsn Posted June 21, 2007 Share Posted June 21, 2007 I would use the glob() function <?php foreach (glob("/dir/to/look/at/*.zip") as $filename) echo $filename . "<br>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279278 Share on other sites More sharing options...
trq Posted June 21, 2007 Share Posted June 21, 2007 I have an error from typing too quickly. if ($ext(count($ext)-1) = 'zip') { should be.... if ($ext[count($ext)-1] = 'zip') { But yeah.... Kens solution is mutch simpler. Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279280 Share on other sites More sharing options...
ztealmax Posted June 21, 2007 Author Share Posted June 21, 2007 ok thanx, now to alter my question somewhat if i want to exclude files to be showned in the list for example if i have some files that ends with .txt and i dont want does files to show up, how would i go about to fix something like that? //Cheer Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279283 Share on other sites More sharing options...
trq Posted June 21, 2007 Share Posted June 21, 2007 You want to display all but .txt ? <?php $dir = "/dir/to/look/at"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $ext = explode('.',$filename); if ($ext[count($ext)-1] != 'txt') { echo $filename."\n"; } } ?> This is all pretty simple logic really. Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279284 Share on other sites More sharing options...
aniesh82 Posted June 21, 2007 Share Posted June 21, 2007 please go through this line code. I tried to simplify the thorpe code while (false !== ($filename = readdir($dh))) { $pattern = '/\.zip$/'; if(preg_match($pattern,$filename)) { /* You can proceed because the file has the extension .zip */ } } Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279287 Share on other sites More sharing options...
ztealmax Posted June 21, 2007 Author Share Posted June 21, 2007 works like a charm if i want it to exlude more file endings how would i do that? Sorry about all the noob Questions Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279293 Share on other sites More sharing options...
ztealmax Posted June 21, 2007 Author Share Posted June 21, 2007 Please if you dont understand my question post it i try and explain better, due to my english isnt that good //Cheers Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279341 Share on other sites More sharing options...
trq Posted June 21, 2007 Share Posted June 21, 2007 To exclude more, just add an or operator in the check. eg; if ($ext[count($ext)-1] != 'txt' || $ext[count($ext)-1] != 'doc') { Quote Link to comment https://forums.phpfreaks.com/topic/56547-show-only-files-that-ends-with-zip-example/#findComment-279343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.