Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What am I missing?

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all,

Ok, here is the CGI file:

#!/usr/bin/perl

opendir (FTPDIR, "../effTeePee/");
print 'YES I GOT THIS FAR!';


## my @folderContents = readdir (FTPDIR);
## closedir (FTPDIR);

## print "Content-type: text/html\n\n";

## if (@folderContents) {
## print 'This directory contains<br><br>';

## foreach (sort @folderContents) {
## print "$_";
## }
## }

As you can see, parts of it are commented out. The reason for this is that I keep getting this error on line 3. I checked the permissions on the directory and they're 755. So what gives? What am I missing?

- MT
 
Hi

I'm happy to see you advancing. Just a little advise for now. Those separate [tt]print[/tt]s make the code abit hard to read. You can output more then one line with one [tt]print[/tt]:

Code:
[gray]# multiline[/gray]
print "<html>
<body>
sample
</body>
</html>";

[gray]# here-document[/gray]
print <<DELIMITER
<html>
<body>
sample
</body>
</html>
DELIMITER

As you can see, in this case the end-of-lines are included, so you don't have to write those [tt]\n[/tt] after each line.

Feherke.
 
Hi

I do not understand what you want with the extension. I think you planed to put them in separate column. I cut out all decorations :

Code:
print "<table border=1>
<tr><th>Type</th><th>Name</th><th>Extension</th><th>Date</th><th>Size</th></tr>\n";

foreach my $indivFile (@folderContents) {
  (($name,$extension)=$indivFile=~m/([^.]+)\.(.*)/) || ($name=$indivFile);
  @info=stat "effTeePee/$indivFile";
  @date=localtime $info[9]; $date[5]+=1900; $date[4]++;
  print "<tr><td>";
  print -d "effTeePee/$indivFile"?"DIR":"file";
  print "</td><td>$name</td><td>$extension</td><td>";
  printf "%02d/%02d/%04d %02d:%02d:%02d",@date[4,3,5,2,1,0];
  print "</td><td>$info[7]</td></tr>\n";
}

print "</table>\n";

Two more things :
[ul]
[li]the [tt]split[/tt] will not work correct for filenames like .htaccess, noextension, pack.tar.gz[/li]
[li]would be better to store the path in a variable, later will be easyer to modify[/li]
[/ul]

Feherke.
 
even easier is to use qq for printing strings, especially if they contain double-quotes:

print qq~<html>
<body>
<a href="page.html">
</body>
</html>
~;

 
Thanks for all the help folks! I'll make sure the the base version of this software is available for those of you who helped me.

OK, here's the next part that I have to deal with:

1) strip out the "." and ".." that are at the front of each of these listings.
2) Make the link an onSubmit which sends contents to another CGI file

Oh, also I have to move things to a private server. !@#$%@#$ Tripod refused to fix my issues.

- MT
 
Almost forgot:

1a) Get the date and size from each file.

- MT
 
Hi

mtorbin said:
strip out the "." and ".." that are at the front of each of these listings.
Don't bother to strip, just skip them when passing through the array :
Code:
foreach my $indivFile (@folderContents) {
  next if $indivFile=~m/^\.+$/;
  [gray]# ...[/gray]
}

mtorbin said:
Get the date and size from each file.
See my previous post, both are included :
Code:
@info=stat "effTeePee/$indivFile";   [gray]# get file info; 7=size, 9=mtime[/gray]
@date=localtime $info[9];            [gray]# transform the UNIX time[/gray]
$date[5]+=1900; $date[4]++;          [gray]# get ready for further use[/gray]
[gray]# ...[/gray]
printf "%02d/%02d/%04d %02d:%02d:%02d",@date[4,3,5,2,1,0]; [gray]# formated output : 08/12/2005 16:41:43[/gray]

Feherke.
 
Here's another aspect of this:

if (@folderContents) {
foreach my $indivFile (@folderContents) {
my $fullUnit = $indivFile;
# escape the following "."
my ($name, $extension) = split (/\./ , $indivFile);
print "<tr valign=top>\n";
print "<td align=center><img src=../images/icon_" . $extension . ".gif width=18 height=22></td>\n";
print "<td><a href=fileDownload.cgi?fileName=" . $fullUnit . ">$fullUnit<a></td>\n";
print "<td>[DATE]</td>\n";
print "<td>[SIZE]</td>\n";
print "</tr>";
}
}

As you can see, the llink will be "fileDownload.cgi?fileName=[SOMETHING]"

So how do I strip off "SOMETHING" and store it in $fileName which is waiting over in fileDownload.cgi? What do I add to fileDownload.cgi?

- MT
 
Also, could you put

next if $indivFile=~m/^\.+$/;

into English for me?

- MT
 
Hi

The substring after the question mark is available in the [tt]QUERY_STRING[/tt] environment variable :
Code:
print $ENV{"QUERY_STRING"};   [gray]# will print : fileName=[SOMETHING][/gray]
So you have to [tt]split[/tt] it.

Feherke.
 
Hi

[tt]next if $indivFile=~m/^\.+$/;[/tt] means :
[tt] next [/tt] skip to the next iteration of current loop
[tt] if [/tt] if
[tt] $indivFile[/tt] value of in variable [tt]$indivFile[/tt]
[tt] = [/tt] is
[tt] ~m [/tt] matching
[tt] / [/tt] regular expresion
[tt] ^ [/tt] from begin of value
[tt] \. [/tt] dot character
[tt] + [/tt] one or more times
[tt] $ [/tt] to end of value
[tt] / [/tt] end of regular expression

Feherke.
 
Hi

Of course, [tt]substr[/tt] is good too, but I prefer this :
Code:
foreach (split /\&/,$ENV{'QUERY_STRING'}) {
  @par=split /=/;
  $par{$par[0]}=$par[1];
}

print $par{"fileName"};

Note, that the query is part of the URL, so is also urlencoded, which means non-alphanumeric characters will be as codes in [tt]%HH[/tt] hexadecimal format. This will do the urldecode ( put it after the above [tt]foreach[/tt] ) :
Code:
foreach (values %par) {
  $_=~s/%(..)/pack('C',hex($1))/eg;
  $_=~s/\+/ /g;
}

Feherke.
 
faster to use:

Code:
next if ($indivFile eq '.' or $indivFile eq '..');

Firing up the regexp engine for this simple task is not necessary and will slow down the script.
 
In reference to your TIME/DATE of file stuff... could you boil that down to a print statement. I still don't follow.

- MT
 
Hi

This ?
[tt]printf "%02d/%02d/%04d %02d:%02d:%02d",@date[4,3,5,2,1,0];[/tt]

[tt]printf[/tt] has two things :
- format string : a string where percent characters mark the places where values will be inserted
- value list : as many percent signs was in the format string

The characters used in the format string [tt]"%02d"[/tt] :
[tt] % [/tt] value to be inserted here
[tt] 0 [/tt] pad with zero, not with space character
[tt] 2 [/tt] pad on left end to this length
[tt] d [/tt] format as signed integer in decimal

In the [tt]printf[/tt]'s format string there are six percent sign, so there must be six values too :
[tt]printf "%02d/%02d/%04d %02d:%02d:%02d",$date[4],$date[3],$date[5],$date[2],$date[1],$date[0];[/tt]
But the value list may be an array, which is more simple in our case, then enumerating scalar variables. So we must arrange somehow the elements of that array to be in the expected order. This can be done by referencing more elements of the same array by enumerating their indexes.

Feherke.
 
if (@folderContents) {
foreach my $indivFile (@folderContents) {
next if $indivFile=~m/^\.+$/;
my $fullUnit = $indivFile;
# escape the following "."
my ($name, $extension) = split (/\./ , $indivFile);

if ($extension) {
@fileInfo=stat "effTeePee/$indivFile";


print "<tr valign=top>\n";
print "<td align=center><img src=../images/icon_" . $extension . ".gif width=18 height=22></td>\n";
print "<td><a href=fileDownload.cgi?fileName=" . $fullUnit . ">$fullUnit<a></td>\n";
print "<td>[DATE]</td>\n";
print "<td>" . $fileInfo[7] . "</td>\n";
print "</tr>";
}

Giving me uninitialized error for the line where I call $fileInfo[7]. Did I do it right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top