The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Get files in a directory & arrange them in a table
Classic07
post Mar 30 2009, 08:24 PM
Post #1





Group: Members
Posts: 4
Joined: 18-March 09
Member No.: 8,103



Hi, this is my first post to this forums. People here seem really helpful, and another site I used to go to, the people there weren't really helpful.

I'm pretty new to PHP and wanted to do some neat things with it, so I set up a home web server where I can practice!

Well this is what I want to do:

I want to get a list of files in a directory:
CODE
if ($handle = opendir("files/download")) {
    
/* Loop De Loop */    
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") { //TY php.net :)
        echo "$file(HTML ~ To setup a table(like Image Below))";
        }
    }
    
    closedir($handle);
}

I used to know some Java, and this is kinda how I read data from a file (looks similar). So I hope that is correct? :S

I want to get each file that it reads from the directory and set up a table in HTML for it (A table that has links to downloads which are the files in the directory).

IPB Image

The reason it's complicated is because I want the script to get the file name, filesize, and output them both into the next corresponding row and under each correct column. The file size I already know how to do.

CODE
$fileName = round(filesize("pathtothefile") / 1048576, 2);


I just don't know how to get them into the right column.

The other way I was thinking is making a function:
CODE
function listNames() {
if ($handle = opendir("files/download")) {
    
/* Loop De Loop */    
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        echo "$file\n";
        }
    }
    
    closedir($handle);
}
}


Then a function to get the fileSize, and just call them in each row that I need them. But I know there's got to be a more efficient way of doing this? Pretty much its for when I decide to add a new file to the folder, it will already have done everything on the HTML page, without much interaction with the page itself(coding, etc), because the script reads the folder, and that's that.

Any thoughts on how I can achieve this?

This post has been edited by Classic07: Mar 30 2009, 09:01 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 31 2009, 08:13 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(Classic07 @ Mar 31 2009, 03:24 AM) *

I want the script to get the file name, filesize, and output them both into the next corresponding row and under each correct column.

If you use a FOR loop the index number of each property should correspond with each table column. First make an outer loop for each file that prints a table row element, then nest a second loop that prints table cell elements for each file's properties.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Classic07
post Mar 31 2009, 03:41 PM
Post #3





Group: Members
Posts: 4
Joined: 18-March 09
Member No.: 8,103



Thanks for the reply.

I still don't entirely understand everything you said, but hopefully I understood the important parts, sorry.
(the index number of each property should correspond with each table column)

So I got this:

CODE

function setTable() {
    $extensions = array('html', 'php', 'htm');
    
    foreach (glob("*.*") as $filename) {
            $files = array($filename => $filename);

$list .= "\n".'<tr><td>' . $filename .'</td></tr>'."\r\n";
    }

    echo '<table border="1">';

    echo '<td>Filename:</td>';

    echo ($list);

    echo '</table>';
}


It comes out exactly what I wanted to, so thanks for that! smile.gif

But now I've run into another problem, it's listing the file(the script in which the function is in). I want to remove any .html, .php, etc files from showing up. So I made an array of the extensions I don't want listed, but it didn't work. Just came up with one column(Filename:).

Oh and yes I did use the array, but I think I used it completey wrong laugh.gif.

CODE

foreach (glob("*.*") as $filename) {
if(!$extensions) { //wow I think I used it as a boolean...
//do table
...
}


I'm still googling for answers.

By the way, Thanks Christian J.

This post has been edited by Classic07: Mar 31 2009, 03:43 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Apr 1 2009, 11:43 AM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Classic07 @ Apr 1 2009, 05:41 AM) *

But now I've run into another problem, it's listing the file(the script in which the function is in). I want to remove any .html, .php, etc files from showing up. So I made an array of the extensions I don't want listed, but it didn't work. Just came up with one column(Filename:).

Oh and yes I did use the array, but I think I used it completey wrong laugh.gif.

CODE

foreach (glob("*.*") as $filename) {
if(!$extensions) { //wow I think I used it as a boolean...
//do table
...
}



Um yes. Well, what does

if(!$extensions)

_mean_?? I suppose you understand that if(P) { xyz } else { pqw } means that if the expression P has a value that PHP regards as "true" (which is something rather messy you need to read about, but basically all values are true except the constant called FALSE, NULL, 0 (and perhaps something I've forgotten). In this case, what is the value of !$extensions?

Remember you are not giving instructions to an intelligent human using a language you don't really speak; you are actually writing down _exactly_ what the system is going to do.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 1 2009, 02:43 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(Classic07 @ Mar 31 2009, 10:41 PM) *

I still don't entirely understand everything you said

Nothing spectacular, just something like:

CODE
echo '<table>
';
for($i=0; $i<count($all_files); $i++)
{
    echo '<tr>
    ';
    for($j=0; $j<count($file_properties); $j++)
    {
        echo '<td>'.$file_properties[$j].'</td>';
    }
    echo '</tr>
    ';    
}
echo '</table>
';

(the example doesn't show how to get the arrays).


QUOTE
CODE

function setTable() {
    $extensions = array('html', 'php', 'htm');
    
    foreach (glob("*.*") as $filename) {
            $files = array($filename => $filename);

$list .= "\n".'<tr><td>' . $filename .'</td></tr>'."\r\n";
    }

    echo '<table border="1">';

    echo '<td>Filename:</td>';

    echo ($list);

    echo '</table>';
}

Can't comment on the FOREACH construct (due to lack of experience), but isn't the resulting table markup incorrect?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Classic07
post Apr 1 2009, 07:35 PM
Post #6





Group: Members
Posts: 4
Joined: 18-March 09
Member No.: 8,103



QUOTE
In this case, what is the value of !$extensions?

$extensions is supposed to be an array including some file extensions to be excluded when making the table.
EX: if the file has .html as the extension it is not to be included in the table.

QUOTE
Can't comment on the FOREACH construct (due to lack of experience), but isn't the resulting table markup incorrect?

Actually, yeah. Thanks for telling me. Wouldn't have noticed it till later.



QUOTE
Remember you are not giving instructions to an intelligent human using a language you don't really speak; you are actually writing down _exactly_ what the system is going to do.


While it's scanning the files in the directory, a parameter would be to see if file has an extension that is in the array $extensions, if it does, then it will be excluded from being listed.


I'll keep researching on how I can achieve this.

Thanks for all the help so far. smile.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Apr 2 2009, 07:29 AM
Post #7


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Classic07 @ Apr 2 2009, 09:35 AM) *

QUOTE
In this case, what is the value of !$extensions?

$extensions is supposed to be an array including some file extensions to be excluded when making the table.
EX: if the file has .html as the extension it is not to be included in the table.



Yes, _I_ can see what you mean by "extensions" -- but the PHP interpreter can't. It's just a meaningless identifier to PHP.

What I meant was, you have written:

if(!$extensions)

Now whether the bit after "if()" gets done or not depends on whether the PHP interpreter evaluates !$extensions as true or false.

And !$extensions evaluates as true if $extensions evaluates as false. But we know exactly what $extensions is, since you have only just created it, as an array of three values. An array of three values is not one of the things (0, NULL, '', etc) that evaluates as false, and therefore the bit after "if()" will _not_ be executed.

That isn't what you meant, is it?

QUOTE



QUOTE
Remember you are not giving instructions to an intelligent human using a language you don't really speak; you are actually writing down _exactly_ what the system is going to do.


While it's scanning the files in the directory, a parameter would be to see if file has an extension that is in the array $extensions, if it does, then it will be excluded from being listed.


What does "a parameter would be" mean? This is really the problem: you can't instruct the PHP interpreter using vague expressions like that.

You need to find out how to get the "extension" part of the filename you are looking at, and see if it is in the array of ones you don't want. Here's some more help:

http://jp2.php.net/manual/en/function.explode.php
http://jp2.php.net/manual/en/function.in-array.php

Please also read:

http://jp2.php.net/manual/en/book.strings.php
http://jp2.php.net/manual/en/book.array.php

The PHP manual is generally very good -- learn to use it!

HTH
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Apr 2 2009, 03:10 PM
Post #8


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



Just a remark, since I'm new (for once) to PHP too : it seems more logical to put the extensions you DO want in the array instead of which you DON'T, as that list might become very long.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 12:00 AM