For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > how to write content of file to an array / variable ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to write content of file to an array / variable ?
I BioKid

2006-07-27, 3:57 am

One simple question -
I need to accept a file from the user and to store it as temp1.
then I need to give this file as an input of another program :
I wrote a script like this, but it is not working : Help ?

#!/usr/bin/perl -w
use CGI;
my $q = new CGI;
my $file = $q->param('file');
print $q->header();

# Reading file from user and writing to another file temp1
open (FOO, ">temp1");
while (<$file> )
{
print FOO $_;
}
close FOO;

# internaly executing xxx program; taking temp1 as input
`/usr/bin/xxx temp1`;

#temp.xxx is output of usr/bin/xxx
@psa = `cat temp1.xxx`;

foreach $i(@psa)
{
print "$i";
print "<br>";
}

--
thanks in advance,
IBK

usenet@DavidFilmer.com

2006-07-27, 3:57 am

I BioKid wrote:
> my $file = $q->param('file');
> while (<$file> )
> {


That's not even close to how CGI handles file uploads. You need to
read up on the module's perldocs:
http://tinyurl.com/ft6el

--
David Filmer (http://DavidFilmer.com)

usenet@DavidFilmer.com

2006-07-27, 3:57 am

use...@DavidFilmer.com wrote:
> That's not even close to how CGI handles file uploads.


Actually, maybe it is, but it's not the best (or safest) way to handle
uploads. Are these plain textfiles (as opposed to binaries)? Are you
using multipart forms to begin with?

Can you tell us what "doesn't work" means?

--
David Filmer (http://DavidFilmer.com)

Paul Lalli

2006-07-27, 6:57 pm

I BioKid wrote:
> One simple question -
> I need to accept a file from the user and to store it as temp1.
> then I need to give this file as an input of another program :
> I wrote a script like this, but it is not working : Help ?


You just asked this question!!!
http://groups.google.com/group/perl...45dc2ccd72b5e18

In that thread, you were told by several people to tell us WHAT is not
working in your script. Why are you ignoring those instructions in
favor of repeating yourself!?

Paul Lalli

Jeff Peng

2006-07-27, 6:57 pm


>my $file = $q->param('file');


Are you sure the $file exist on your system actually?
>


>open (FOO, ">temp1");

Under CGI,you maybe always need to specify a full path to the file.Maybe you
would write:
open Foo,">/your/path/temp1" or die $!;

>while (<$file> )

Before reading,you should open the $file at first.And,the full path to the
$file is important.


>`/usr/bin/xxx temp1`;


The same,where is the temp1?Does your webserver has the privileges to
access/write it?

At last,the most important thing to debug a program under CGI is that you
should always take a look at webserver's error_log.From error_log you should
find most of problems happened with you.And,add 'use strict' at the begin of
your script please.



--Jeff Peng
http://pobox.com/~jeffpeng


Michael Gale

2006-07-28, 9:56 pm

Hello,

Here is what I currently do to accept file uploads from users via http:

#!/usr/bin/perl -w

use CGI;

$upload_dir = "/tmp";

$query = new CGI;

$filename = $query->param("fname");
$filename =~ s/.*[\/\\](.*)/$1/;
##
$upload_filehandle = $query->upload("fupload");

if ( $filename =~ m/match my requirements for naming/ ) {

if ( -e "$upload_dir/$filename" ) {
unlink $upload_dir/$filename;
}

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;

$error = "Done\n";

} else {
$error = "Invalid name\n";
}

print $query->header ( );
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading!</P>
<P>$error

</BODY>
</HTML>

END_HTML

Also here is the html code used:

<HTML>
<HEAD></HEAD>
<BODY>
<FORM ACTION="/cgi-bin/filerecev.cgi" METHOD="post"
ENCTYPE="multipart/form-data">
File to Upload: <INPUT TYPE="file" NAME="fupload">
Filename: <INPUT TYPE="text" NAME="fname">
<br><br>
<INPUT TYPE="submit" NAME="Submit" VALUE="OK">
</FORM>
</BODY>
</HTML>



I BioKid wrote:
> One simple question -
> I need to accept a file from the user and to store it as temp1.
> then I need to give this file as an input of another program :
> I wrote a script like this, but it is not working : Help ?
>
> #!/usr/bin/perl -w
> use CGI;
> my $q = new CGI;
> my $file = $q->param('file');
> print $q->header();
>
> # Reading file from user and writing to another file temp1
> open (FOO, ">temp1");
> while (<$file> )
> {
> print FOO $_;
> }
> close FOO;
>
> # internaly executing xxx program; taking temp1 as input
> `/usr/bin/xxx temp1`;
>
> #temp.xxx is output of usr/bin/xxx
> @psa = `cat temp1.xxx`;
>
> foreach $i(@psa)
> {
> print "$i";
> print "<br>";
> }
>


--
Michael Gale

Red Hat Certified Engineer
Network Administrator
Pason Systems Corp.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com