Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Im trying to get my script to let users change and upload into sub-DIR's and also for the script to add the size of the files in the sub DIR's to the file in the root DIR and i would to print the list like this

This is the code im using now
@dir = sort {lc($a) cmp lc($b)} @dir; foreach $line (@dir) { if (-f "$config{'root_dir'}/$in{'username'}/$line") { @stat=stat("$config{'root_dir'}/$in{'username'}/$line"); if ($stat[7] > 1000) { $stat[7] *= .001; $stat[7] = int($s +tat[7]); $stat[7] .= " KB"; } else { $stat[7] = int($stat[7]); $stat[7] .= " Bytes"; } $stat[9] = localtime($stat[9]); $list .= qq~ <tr> <td width="30"><input type="radio" value="$line" name="file" onclick=" +document.files.file_input.value='$line'"></td> <td width="250"><font face="Arial"><a href="$config{'root_url'}/$in{'u +sername'}/$line" target="_window">$line</a></font></td> <td width="100"><font face="Arial">$stat[7]</font></td> <td><font face="Arial">$stat[9]</font></td> </tr>~;

I have setup a temp accout so that you can see what it dose now and maby it will help you see what i mean. CLICK HERE just use this to log-ig

Thank You

Replies are listed 'Best First'.
Re: how can i also show sub folders
by tstock (Curate) on Nov 21, 2001 at 13:21 UTC
    A few notes:

    . You can avoid doing stat twice by using the _(underscore) handle:
    @stat = stat(_); # the -f test did a stat on the same file

    . you can use spritf to simplify your code:
    if ($stat[7] > 1000) { $stat[7] = sprintf( "%d KB", int($stat[7] * .001) ); }

    Someone else in this monastery will mention the use strict, use warnings, -w,, HTML::Template stuff :)

Re: how can i also show sub folders
by Zaxo (Archbishop) on Nov 21, 2001 at 13:14 UTC

    First, I'd like to warn you to look above this snippet for security problems. I don't see anything here to say you run this in taint mode, or use CGI; for query parsing. You may be doing that already, which is good.

    File::Find is a great help in recursing through directory trees. It will do the right thing with symlinks, and can handle all the file tests you need to do.

    If you have many, you may want to consider a Schwartzian Transform for a case insensitive sort.

    Update: Re your reply:

    1. has been superceded by, which is a modern standard. Your query parsing will be improved by using it.
    2. You should make use of the server's Basic Authentication, rather than handling it in your script. Consider using https for this.
    3. The -wT options on the shebang line will turn on warnings and taint mode. Taint checking forces you to examine user input for damaging stuff. In this case, can a user send '../../../../*' and wipe clean your mounted filesystems?
    4. use strict; as always. It will frustrate you at first, but it is truly worth it. It prevents some sneaky bugs and spots many typos.
    'perldoc perlsec' is a good read. I also regard the article phrack 55.7 as required reading. It is a real eye-opener.

    After Compline,

      Now nbot to fell self righteous here, but I really did not like File::Find when I tried to implement it so I choose to make my one recursive dir program:revised Reply to that program which you could modify to do what you wish here. I do believe that for your purposes that File::Find might be suficient for you. Who knows just a suggestion. Clovs aka jclovs
      well about the security problems im new to CGI but this is what i have @ the top

      use Fcntl; use AnyDBM_File; require ""; &ReadParse(\%in,\%cgi_cfn); print "Content-type: text/html\n\n"; if (!$in{'username'} || !$in{'password'}) { &login_html; exit; } &get_values; &check_login; &actions; &manager_html;
      i dont know if this is better or worse then what you told me.