Forum Moderators: coopster & phranque

Message Too Old, No Replies

File Uploading Script

         

stevethomas67

7:05 pm on May 31, 2001 (gmt 0)



I found this thread searching on information concerning a problem I'm having with <INPUT TYPE=FILE... and hopefully someone here can help.

I'm fairly green in terms on HTML, only been coding for about 6 months (in regards to HTML).

I'm writing a simple conversion utility to convert UNIX timestamps to normal everyday dates. Since <TEXTAREA> has a size limitation I have decided to use TYPE=FILE to upload a text file containting the UNIX timestamps into the CGI script and let the perl script process it from there. When I Browse to the text file, I always get:

"Error opening C:\unixtime.txt: No such file or directory"

I'm using a typical installation of IIS 5, do I need any other "plug-ins" whatever to get this to work? Or should it work "right out of the box"?

One other note... I'm not using ENCTYPE="multipart/form-data", because I'm sending this data right back to the same script.

My Incompleted HTML CODE:

#!i:/perl/lib -w

use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);

# Create new CGI object and print standard CGI header
$q = new CGI;
print $q->header;

$action = $q->param(action);
$filename = $q->param(filename);

print "<FORM METHOD='post' ACCEPT='text/html' ACTION='$form_action'>";
print "Select File to Convert from UNIX TIMESTAMP Format to Normal DATE Format:<BR>";
print "<INPUT TYPE=FILE NAME='filename' SIZE='75'><BR>";
print "<BR><P></P>";
print "<INPUT TYPE='submit' NAME='action' VALUE='Convert File'>";

if($action =~ /convert/i) {&convert;}

print "</FORM>";

exit;

############## Begin Subroutines

sub convert {

print "<P></P>$filename<BR>";

open(UNIXFILE, "<$filename") or die "Error opening $filename: $!\n";
@unixdata = <UNIXFILE>;
close(UNIXFILE);

print "<P></P>";
print "@unixdata";

#$normaldate = scalar localtime($unix)."\n";

# END of Code

Thanks,
Steve Thomas

Air

5:24 am on Jun 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On NT you generally need to escape the path slashes in files, so:

C:\unixtime.txt

should be C:\\unixtime.txt

littleman

6:04 am on Jun 1, 2001 (gmt 0)



Try this.

#!/usr/bin/perl
use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);

# Create new CGI object and print standard CGI header
$q = new CGI;
print $q->header;

$action = $q->param(action);
$filename = $q->param(filename);

print "<FORM METHOD='post' ACCEPT='text/html' ACTION='$form_action'>";
print "Select File to Convert from UNIX TIMESTAMP Format to Normal DATE
Format:<BR>";
print "<INPUT TYPE=FILE NAME='filename' SIZE='75'><BR>";
print "<BR><P></P>";
print "<INPUT TYPE='submit' NAME='action' VALUE='Convert File'>";

if($action =~ /convert/i) {&convert;}

print "</FORM>";

exit;

############## Begin Subroutines

sub convert {

print "<P></P>$filename<BR>";

$normaldate = scalar localtime($filename)."\n";
print $normaldate;
}
# END of Code

littleman

6:12 am on Jun 1, 2001 (gmt 0)



The main problem was that '$q->param(filename);" was already opening the document and viewing the content. So the script was trying to open the data inside the the file instead of the file itself.
You may need to reset the path to perl. Do you need to save the data locally on the NT box also?

stevethomas67

1:03 pm on Jun 1, 2001 (gmt 0)



All I need it to do is convert a large list of UNIX Timestamps to normal dates. Large list, meaning ~50k entries, lines, individual timestamps to ~50,000 normal dates. I would have used <TEXTAREA>, but it has a ~32k character limitation. So it seems my only alternative is to import a populated text file and have the perl script generate the data, either to another text file, or right to the browser (and I can cut and paste from there.)

I've tried using the double backslashes, but doesn't work. I've even tried hardcoding in the filename. As long as the file is on the IIS server (i:/inetpub/times/unixtime.txt), it works, but I can't get it to recognize a file on MY system.

I'm dealing with Log files here... machines that spit out a total of 2GB of text data a day. And all sequenced with UNIX timestamps, but to get it in a readable format, I need to see it in normal dates, and so do the VP's.

I can do it all manually, the perl script that has the file local on the server and the filenames coded into the perl script, but I want a little more flexibility.

Thank you all for your help.
Steve

sugarkane

1:36 pm on Jun 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing $filename to the absolute server path - ie:

$filename="i:/inetpub/times/$filename";

before you open it in the convert routine.

(Note: comments about double slashes will probably still apply - I don't know <g>)

stevethomas67

1:42 pm on Jun 1, 2001 (gmt 0)



I want to keep the functionality of chosing a file from my local system... that way, if someone else wants to use the process, they don't have to have right/permissions to the IIS server (assuming they don't know anything about uuencoding and IIS 5 exploits.) I'd even consider having the script copy the file locally and crunch on it from there (the IIS server), but if I can copy it from my PC, why can't I just USE it from my PC.

Thanks.

sugarkane

3:24 pm on Jun 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> just USE it from my PC

I don't think this is possible. The usual way to do things is to upload into a temporary file, copy that file into a specific location and then do your crunching on it (then maybe delete it).

AFAIK there's no way around it, your script has to have write permissions for some directory on the server.

littleman

6:18 pm on Jun 1, 2001 (gmt 0)



This will print the converted times into a browser. It assumes the times are separated like this:
123321513215
132421513215
654654654654
654654654652
I haven't tried it on NT, but it should work on that platform as well.

#!/usr/bin/perl
use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);

# Create new CGI object and print standard CGI header
$q = new CGI;
print $q->header;

$action = $q->param(action);
$filename = $q->param(filename);
unless ($filename ) {
print "<FORM METHOD='post' ACCEPT='text/html' ACTION='$form_action'>";
print "Select File to Convert from UNIX TIMESTAMP Format to Normal DATE
Format:<BR>";
print "<INPUT TYPE=FILE NAME='filename' SIZE='75'><BR>";
print "<BR><P></P>";
print "<INPUT TYPE='submit' NAME='action' VALUE='Convert File'>";

print "</FORM>";
exit;
}
else {&convert;}

############## Begin Subroutines

sub convert {

print "<P>";

print '<pre>';
@times = split(/\n/,$filename);
foreach $time(@times){
my $normaldate = scalar localtime($time);
print $normaldate . "\n";
}
print '</pre>';
exit;
}
# END of Code

stevethomas67

6:45 pm on Jun 1, 2001 (gmt 0)



Thank you. I tried that, but I'm still having trouble getting the process to get the contents of $filename into the script. I use a text file that has about 15 timestamps, but the new script keeps returning "Wed Dec 31 19:00:00 1969". It's not pulling in the data from the file. This may in fact be an NT ACTIVESTATE (Perl emulator) problem.

My main problem is getting the IIS server/script to pull the data from my local harddrive. When the IIS/Perl script processes $filename, I believe it is trying to pull it from a local (server) location. When $filename is "C:\unixtime.txt", it doesn't realize $filename is local to the Browsing PC, it seems to want to from it's "C:\", which it doesn't exist. Even when I try to incorporate a copy of the local file to the server (within the script), it still tries to copy it from "C:\" from itself, and not the Browsing PC. Follow me?

Again, Thanks.

stevethomas67

6:53 pm on Jun 1, 2001 (gmt 0)



littleman...

How are you setup? Meaning, are you running this script on a web server and accessing the the script through a browser on another computer? Which browser? The way your example script is, does it work properly by collecting the data from the client PC? That's my specific problem... it's not collecting the data from the client PC.

stevethomas67

7:17 pm on Jun 1, 2001 (gmt 0)



And another thing...

All of the references I've seen concerning useing TYPE="FILE" (in regards to <INPUT>), they state you must use ENCTYPE="multipart/form-data" parameter within the <FORM> tag. And so far nobody has complained that I haven't used it in my sample code... is it really needed? I've tried using it, and I get an interesting permission error in regard to "tmpfile". I've granted the webuser rights to write to the directory, but it still is giving me fits. Go figure.

littleman

2:38 am on Jun 2, 2001 (gmt 0)



wow, that is really weird. It works fine in a unix to unix situation, but MSIE gives me the same error you are getting.

Air

2:42 am on Jun 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Couple of quick suggestions:

If I'm getting the jist of this ... would a share do the trick? that way the file path could address the file location correctly, might have some security concerns though....

Or use LWP to "get" the file to the local machine then run your script on successful retrieval?

<added>now that I re-read this, neither of those may be useful :( </added>

littleman

6:01 am on Jun 2, 2001 (gmt 0)



[cgi-fun.hypermart.net...]
[cgi-fun.hypermart.net...]
I tested it using windows MSIE and netscape, it worked. Let me know if it does/doesn't work for you on your NT box.

stevethomas67

4:28 pm on Jun 5, 2001 (gmt 0)



I think I've boiled it down to an NT rights/permission issue. I found some information on another thread concerning my "CGI open of tmpfile: Permission Denied" error. It indicated that I need to give my computer access rights to C:\TEMP on the webserver itself, for that is where the "tmpfile" is stored while the script runs. My PC isn't a member of the domain, so I can't give it explicit rights to C:\TEMP on the webserver... but can I change the location of the "tmpfile" so it resides in the script section which is already setup with such rights for the duration of the script or whatever?

I realize most of you guys use Unix (or some variation), and I would too if I had the opportunity. But NT is all I got to work with. Thanks.