For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > File handling using CGI









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 File handling using CGI
I BioKid

2006-07-26, 6:57 pm

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

Paul Lalli

2006-07-26, 6:57 pm

I BioKid wrote:
> One simple question -


Yes. Far too "simple" of a question. If you want good help, consider
putting some time into asking a well-thought-out question, rather than
a simple one.

> 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 ?


"is not working" is the worst of all possible error descriptions. *HOW*
is it not working? What results do you see that you don't want, or do
you not see that you do want? Are you getting a syntax error?
Run-time error? Freeze? Segfault? Infinite loop? Computer bursts
into flames?!

>
> #!/usr/bin/perl -w


You forgot:
use strict;
and
use warnings;
is far preferred to -w these days.

> use CGI;


you forgot:
use CGI::Carp qw/fatalsToBrowser/;

> 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");


No checking to see if this file successfully opened. Using global
barewords instead of lexicals. Using the two argument form of open.
All BAD.

open my $FOO, '>', 'temp1' or die "Cannot open temp1: $!";

> while (<$file> )
> {
> print FOO $_;
> }
> close FOO;
>
> # internaly executing xxx program; taking temp1 as input
> `/usr/bin/xxx temp1`;


perldoc -q backticks
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq8.pod
What's wrong with using backticks in a void context?

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


Storing the entire contents of this file in memory....

>
> foreach $i(@psa)


.... only to loop through it line by line. Why?

$i is traditionally used as a counter variable. Using it for each
element of an array is confusing.

> {
> print "$i";


perldoc -q quoting
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
What's wrong with always quoting "$vars"?

> print "<br>";
> }


open my $fh, '<', 'temp1.xxx' or die "Cannot open file 'temp1.xxx':
$!";
while (<$fh> ) {
print "$_<br>";
}
close $fh;

> thanks in advance,


For what?! You never bothered to tell us what was wrong with your
script!

Paul Lalli

Prabu

2006-07-27, 3:57 am

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>";
> }
>

Can u explain whats your requirement little more clearly.

You mean reading the file name from the user and copying the file to
temp1 and then using this temp1.

then you can use the

`cp file1 temp1`

and then to use it in another file.

If not please explain it little more :(

--
Prabu.M.A
When I was born I was so surprised
I didnt talk for a period and half
-Gracie Allen

I BioKid

2006-07-27, 3:57 am

Dear Prabu and all,

My purpose is simple,
I have a web-form, that will accept a file,
I need to use this file as an input of a program in my server,
then I need to print the out put on the web page.

I am able to do the second part, but am not able to get the users file to a
variable or array ?
I just want to know how to pass the users file to a file called temp in my
server.

I tried this many time :( - pls help
--
thanks in advance !!!

Charles K. Clarkson

2006-07-27, 3:57 am

I BioKid wrote:

: I am able to do the second part, but am not able to get
: the users file to a variable or array ?

Read the "Files and I/O" section of the "perlintro"
file in the perl documentation.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

Ken Foskey

2006-07-27, 6:57 pm

On Wed, 2006-07-26 at 23:57 +0530, 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>";
> }



I think that you may want to look up pipes. You can pass in data to a
program, have it process it and then get the output out from that
program without using an intermediate file.

I am curious why you wrote this:
[color=darkred]

When you had this a little earlier in your code.
[color=darkred]


Sponsored Links







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

Copyright 2008 codecomments.com