Jump to content

[SOLVED] creating function to diplay file size getting errors


farkewie

Recommended Posts

Hello,

 

i am trying to create a page that reads a directory full of files and folders and i want to display the size of each file. i dont think i can display the size of folders,

 

but im getting errors on all of them , i also want to create a link to all files for download and all folders to open that directory and read files in there and display the same as above

 

things i cant work out.

 

why im getting the filesize errors ?

how to tell if an item is a file or folder?

 

after that i should be able to get the rest if any one can point me in the right direction for the above it would be great here is my code so far

 



			  <?php

/**
* @author Tyron Gower
* @copyright 2007
*/
/**
* bigfiles()
*
* @param mixed $file
* @return
*/
function bigfiles($file)
{
    $org = filesize($file);
    $bigfile = ($org / 1024 / 1024);
    return $bigfile;
}

$dir = "z:\Torrents";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    if ($filename == '.' || $filename == '..') {
        continue;
    }
    $files[] = $filename;
}

sort($files);
rsort($files);
$file_count = count($files);
$newcol = ($file_count / 2);
$newcol = floor($newcol);
$count = 0;
echo <<< EOT
<div class="boxed">
		<h2>Finished Torrents</h2>
		<div class="content">
		<table align="left" valign="top"> <tr><td><ul>
EOT;

foreach ($files as $file) {
    if ($count == $newcol) {
        echo "</ul></td><td align=\"left\" valign=\"top\"><ul>";
    }
    echo "<li>" . wordwrap($file, 15) . "  " . bigfiles($file) . "</li>";
    $count++;
}
echo "</ul></td></tr></table>  </div>
  </div>";

?>

 

Thanks Ty..

OK i managed to get rid of the errors, but i also need to make a function that will tell me if file is a file or a folder?

 

i want to create links for each one so..

 

if file i can download the file

and if link

 

create link like $_SERVER['PHP_SELF'] . "?dir=" . $file

 

so i can then brows through the next folder.

 

i know it will work i have manually type the address in and all works fine

 

 

here is my code so far

 



<?php

function bigfiles( $file, $dir )
{

     $org = filesize( $dir . "/\/" . $file );
     $bigfile = ( $org / 1024 / 1024 );
     $bigfile = round( $bigfile, 2 );
     echo $bigfile . " MB";
}
if ( isset($_GET['dir']) )
{
     $dir = "z:\Torrents/\/" . $_GET['dir'];
}
else
{
     $dir = "z:\Torrents";
}
$dh = opendir( $dir );
while ( false !== ($filename = readdir($dh)) )
{
     if ( $filename == '.' || $filename == '..' )
     {
         continue;
     }
     $files[] = $filename;
}

sort( $files );
sort( $files );
$file_count = count( $files );
$newcol = ( $file_count / 2 );
$newcol = floor( $newcol );
$count = 0;
echo <<< EOT
<div class="boxed">
		<h2>Finished Torrents</h2>
		<div class="content">
		<table align="left" valign="top"> <tr><td><ul>
EOT;

foreach ( $files as $file )
{
     if ( $count == $newcol )
     {
         echo "</ul></td><td align=\"left\" valign=\"top\"><ul>";
     }
     echo "<li>" . wordwrap( $file, 15 );
     echo " <font color=\"red\"> ";
     bigfiles( $file, $dir );
     echo "</font></li>";

     $count++;
}
echo "</ul></td></tr></table>  </div>
  </div>";

?>

I have it all working now just took a lot of reading  ;D

 

this code will read 2 directorys deep eg

 

main <-- i can download files from here

main folder1 <-- i can download files from here

main folder1 folder2  <-- i can download files from here

 

here is my final code it may be using simple logic but it works

if anyone has a better smarter way of doing it i would love to hear im always up for learning.

 


<?php

/**
  * @author Tyron Gower
  * @copyright 2007
  */
function bigfiles( $file, $dir )
{

     $org = filesize( $dir . "/\/" . $file );
     $bigfile = ( $org / 1024 / 1024 );
     $bigfile = round( $bigfile, 2 );
     echo $bigfile . " MB";
}


function createLinks( $file, $dir, $indir )
{
     if ( is_dir($dir . "/\/" . $file) )
     {
         echo "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?dir=".$indir . "/". $file . "\">" . $file .
             "</a></li>";

     }
     else
     {
         if ( ! empty($indir) )
         {
             echo "<li><a href=\"/torrents/" . $indir . "/" . $file . "\">" . $file .
                 "</a></li>";
             echo " <font color=\"red\"> ";
             bigfiles( $file, $dir );
             echo "</font></li>";

         }
         else
         {
             echo "<li><a href=\"/torrents/" . $file . "\">" . $file . "</a></li>";
             echo " <font color=\"red\"> ";
             bigfiles( $file, $dir );
             echo "</font></li>";
         }
     }
}

if ( isset($_GET['dir']) )
{
     $indir = $_GET['dir'];
     $dir = "z:\Torrents/\/" . $_GET['dir'];
}
else
{
     $dir = "z:\Torrents";
}
$dh = opendir( $dir );
while ( false !== ($filename = readdir($dh)) )
{
     if ( $filename == '.' || $filename == '..' )
     {
         continue;
     }
     $files[] = $filename;
}

sort( $files );
sort( $files );
$file_count = count( $files );
$newcol = ( $file_count / 2 );
$newcol = floor( $newcol );
$count = 0;
echo <<< EOT
<div class="boxed">
		<h2>Finished Torrents</h2>
		<div class="content">
		<table width="690" align="left" valign="top"> <tr><td width="345"align="left" valign="top"><ul>
EOT;

foreach ( $files as $file )
{
     createLinks( $file, $dir, $indir );
     if ( $count == $newcol )
     {
         echo "</ul></td><td width=\"345\" align=\"left\" valign=\"top\"><ul>";
     }


     $count++;
}
echo "</ul></td></tr></table>  </div>
  </div>";

?>

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.