Supportnet Computer
Planet of Tech

Supportnet / Forum / Skripte(PHP,ASP,Perl...)

PHP > Array Sortier Problem





Frage

Ich hab ein Script das Dateien aus einem Verzeichnis liest und ausgibt. Ich will aber das er die ausgegebenen Dateien erst sortiert und dann ausgibt. Hoffentlich kann mir wer helfen .. PS: Hier ist mein bisheriges Script: <?php $path = $_GET[´path´]; $sort_array = Array(); if(!isset($path)) { $path = "load"; } if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $fName = $file; $file = $path.´/´.$file; if(is_file($file)) { print "<a href=´".$file."´>".$fName."</a> (".filesize($file)." bytes)<br>"; } } } closedir($handle); } ?>

Antwort 1 von sPin

edit: sortierung soll alphabetisch kommen.

Antwort 2 von snak3

hi, das sollte funktionieren:

<?php
function array_csort_cmp($a, $b) {
return strnatcasecmp($a[1], $b[1]);
}

function array_csort($aArray,$cSort=SORT_ASC) {
usort($aArray, "array_csort_cmp");
if($cSort == SORT_DESC) {
return array_reverse($aArray);
}
return $aArray;
}

function readDirFiles($path) {
$sort_array = Array();
if(!isset($path))
{
$path = "load";
}
if ($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fName = $file;
$file = $path.´/´.$file;
if(is_file($file))
{
$sort_array[] = array($file,$fName,filesize($file));
}
}
}
closedir($handle);
}
$sort_array = array_csort($sort_array);
$sReturn = "";
foreach ($sort_array as $aFile) {
$sReturn .= "<a href=\"".$aFile[0]."\">".$aFile[1]."</a> (".$aFile[2]." bytes)<br>";
}
return $sReturn;
}

print readDirFiles($_GET[´path´]);
?>

Antwort 3 von snak3

beim oberen hat er was unterschlagen ^^
hier nochmal
 <?php 
function array_csort_cmp($a, $b) {
	return strnatcasecmp($a[1], $b[1]);
}

function array_csort($aArray,$cSort=SORT_ASC) {
	usort($aArray, "array_csort_cmp");
	if($cSort == SORT_DESC) {
		return array_reverse($aArray);
	}
	return $aArray;
}

function readDirFiles($path) {
	$sort_array = Array();
	if(!isset($path))
	{
		$path = "load";
	}
	if ($handle = opendir($path))
	{
		while (false !== ($file = readdir($handle)))
		{
			if ($file != "." && $file != "..")
			{
				$fName = $file;
				$file = $path.´/´.$file;
				if(is_file($file))
				{
					$sort_array[] = array($file,$fName,filesize($file));
				}
			}
		}
		closedir($handle);
	}
	$sort_array = array_csort($sort_array);
	$sReturn = "";
	foreach ($sort_array as $aFile) {
		$sReturn .= "<a href=\"".$aFile[0]."\">".$aFile[1]."</a> (".$aFile[2]." bytes)<br>"; 
	}
	return $sReturn;
}

print readDirFiles($_GET[´path´]);
?>  


Antwort 4 von snak3

geht irgendwie nich....
nochmal
 <?php 
function array_csort_cmp($a, $b) {
	return strnatcasecmp($a[1], $b[1]);
}

function array_csort($aArray,$cSort=SORT_ASC) {
	usort($aArray, "array_csort_cmp");
	if($cSort == SORT_DESC) {
		return array_reverse($aArray);
	}
	return $aArray;
}

function readDirFiles($path) {
	$sort_array = Array();
	if(!isset($path))
	{
		$path = "load";
	}
	if ($handle = opendir($path))
	{
		while (false !== ($file = readdir($handle)))
		{
			if ($file != "." && $file != "..")
			{
				$fName = $file;
				$file = $path.´/´.$file;
				if(is_file($file))
				{
					$sort_array[] = array($file,$fName,filesize($file));
				}
			}
		}
		closedir($handle);
	}
	$sort_array = array_csort($sort_array);
	$sReturn = "";
	foreach ($sort_array as $aFile) {
		$sReturn .= "<a href=\"".$aFile[0]."\">".$aFile[1]."</a> (".$aFile[2]." bytes)<br>"; 
	}
	return $sReturn;
}

print readDirFiles($_GET[´path´]);
?> 


Antwort 5 von sPin

da kommt eine fehlermeldung wenn ich das script so reinschreibe:

Parse error: parse error, unexpected ´}´ in /www/htdocs/v080040/intern/index.php on line 230


(mit Line 230 ist die Zeile return strnatcasecmp($a[1], $b[1][] = array($file,$fName,filesize($file)); gemeint

Antwort 6 von snak3

ich weis... da is nut die hälfte drinne das forum spinnt irgendiwe...

Antwort 7 von sPin

also ich hab ein funktionierendes Script von snak3 bekommen ... wer eines möchte der melde sich bei icq bei mir!

Ich möchte kostenlos eine Frage an die Mitglieder stellen:


Ähnliche Themen:


Suche in allen vorhandenen Beiträgen: