Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how can i also show sub folders

by Anonymous Monk
on Nov 21, 2001 at 12:31 UTC ( [id://126738]=perlquestion: print w/replies, xml ) Need Help??

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
A_dir
B_dir
A_file
B_file

===============================================
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
username-temp
password-temp

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, CGI.pm, HTML::Template stuff :)

    Tiago
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. cgi-lib.pl has been superceded by CGI.pm, 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,
    Zaxo

      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 "cgi-lib.pl"; &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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://126738]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found