Wernch
Mitglied
Aktiver User
Dabei seit: 01.08.2007
Herkunft: Österreich
Posts: 207
Ermittelt den freien Speicherplatz.
Ich weiß nicht ob das einer braucht!
Das hier ist ein script mitdem man den Speicherplatz in GB und % ausgeben kann:
PHP
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
<?php
function getByte ( $bytes ) {
$symbol = "Bytes" ;
if ( $bytes > 1024 ) {
$symbol = "KiB" ;
$bytes /= 1024 ;
}
if ( $bytes > 1024 ) {
$symbol = "MiB" ;
$bytes /= 1024 ;
}
if ( $bytes > 1024 ) {
$symbol = "GiB" ;
$bytes /= 1024 ;
}
$bytes = round ( $bytes , 2 );
return $bytes . $symbol ;
}
function getFreespace ( $path ) {
if ( preg_match ( "#^(https?|ftps?)://#si" , $path )) {
return false ;
}
$freeBytes = disk_free_space ( $path );
$totalBytes = disk_total_space ( $path );
$usedBytes = $totalBytes - $freeBytes ;
$percentFree = 100 / $totalBytes * $freeBytes ;
$percentUsed = 100 / $totalBytes * $usedBytes ;
echo "Speichertotal: " . getByte ( $totalBytes ). "<br />" ;
echo "Belegter Speicher: " . getByte ( $usedBytes );
printf ( " (%01.2f%%)" , $percentUsed );
echo "<br />" ;
echo "Freier Speicher: " . getByte ( $freeBytes );
printf ( " (%01.2f%%)" , $percentFree );
}
//Usage
getFreespace ( "." );
?>
Viel Spaß damit!
Post wurde schon 2x editiert, das letzte mal am 22.02.2008 um 20:22 von Andavos
29.09.2007, 15:09
Profil |
PM |
E-Mail
Lukas der Krasse
Mitglied
Guter User
Dabei seit: 18.06.2007
Herkunft: Österreich
Posts: 420
Guter Script!! Funktioniert super! Vielen Dank!
29.09.2007, 15:10
Profil |
PM |
E-Mail
Wernch
Mitglied
Aktiver User
Dabei seit: 01.08.2007
Herkunft: Österreich
Posts: 207
bitteschön!
29.09.2007, 15:19
Profil |
PM |
E-Mail
Rudelwolf
Mitglied
Sehr guter User
Dabei seit: 04.08.2006
Herkunft: Geislingen an der Steige
Posts: 553
wenn du schon $path benutzts dann bitte auch immer. Und C: kann man so nicht auf jedem Server benutzen. ersetz das mal mit /
29.09.2007, 19:44
Profil |
PM |
E-Mail
Teralios
Moderator
Perfekter User
Dabei seit: 18.09.2005
Herkunft: Berlin
Posts: 2542
Ok, hab mich mal hingesetzt und es ein wenig verbesser.
PHP
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
<?php
function getByte ( $bytes ) {
$symbol = "Bytes" ;
if ( $bytes > 1024 ) {
$symbol = "KiB" ;
$bytes /= 1024 ;
}
if ( $bytes > 1024 ) {
$symbol = "MiB" ;
$bytes /= 1024 ;
}
if ( $bytes > 1024 ) {
$symbol = "GiB" ;
$bytes /= 1024 ;
}
$bytes = round ( $bytes , 2 );
return $bytes . $symbol ;
}
function getFreespace ( $path ) {
if ( preg_match ( "#^(https?|ftps?)://#si" , $path )) {
return false ;
}
$freeBytes = disk_free_space ( $path );
$totalBytes = disk_total_space ( $path );
$usedBytes = $totalBytes - $freeBytes ;
$percentFree = 100 / $totalBytes * $freeBytes ;
$percentUsed = 100 / $totalBytes * $usedBytes ;
echo "Speichertotal: " . getByte ( $totalBytes ). "<br />" ;
echo "Belegter Speicher: " . getByte ( $usedBytes );
printf ( " (%01.2f)%%" , $percentUsed );
echo "<br />" ;
echo "Freier Speicher: " . getByte ( $freeBytes );
printf ( " (%01.2f)%%" , $percentFree );
}
?>
Beide Funktionen sind Wichtig.
Die Erste berechnet den Wert eines Bytes mit dem richtigen Binärsuffix (Prefix ist ja davor, in dem fall wirds ja angehängt )
Diese Funktion könnt ihr auf jede Bytezahl anweden, es wird immer auf 2 Stellen gerundet.
Die zweite Funktion macht nichts anderes, als der obenbeschrieben Teil, nur das es 3 Werte Ausgibt.
Gesammterpseicher
Belegterspeicher
Freispeicher und die Prozenz angabe.
Zudem wird sofort überprüft, ob etwas eine Url sein könnte, denn dann nützt die Funktion nichts.
An Wernch... bitte mach keine diskfreespace("$path") das ist sinnlos und beeinflust die preformance negativ. Natürlich merkt man das bei einmal nicht, aber bei mehreren merkt man es.
29.09.2007, 22:32
Profil |
PM |
E-Mail