Jump to content

Show only files that ends with .zip (example)...


ztealmax

Recommended Posts

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

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 :D

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.

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      */

 

}

}

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.