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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if/subs isn't working

Status
Not open for further replies.

sulfericacid

Programmer
Aug 15, 2001
244
0
0
US
The script below was over 200 lines earlier. I posted everything in the sub into each if originally and thought it was too messy and decided to try and shorten the script. My idea was to make all the real processing instructions or whatever you want to call it inside the subroutine and just call it for each <b>if</b>/filefield.

I used $num++ originally instead of $field which worked but that had one problem. If you filled in field3 and NOT field2 or field1 it would error out because the $field$num wouldn't match up.

My next idea was to use static numbers or the fields (as you see below). I use $field = 1 for param('upload1'), $field = 2 for param('upload2') and so on. This way SHOULD work because it doesn't matter which fields they have data in, they all have their own static numbers to work on. BUT SOMETHING DOESN'T WORK! It used to upload files when using $num but now it doesn't. It tries to upload and refreshes the screen but nothing happens. Can someone figure out why?




if ( param('upload1') ) {

my $field = &quot;1&quot;;
&dirty_work;
}

if ( param('upload2') ) {

my $field = &quot;2&quot;;
&dirty_work;
}

if ( param('upload3') ) {

my $field = &quot;3&quot;;
&dirty_work;
}

if ( param('upload4') ) {

my $field = &quot;4&quot;;
&dirty_work;
}







sub dirty_work {
# take form data
my $remotefile = param(&quot;upload$field&quot;);

# make new variable to prevent overwriting of form data
my $filename = $remotefile;

# remove all directories in the file name path
$filename =~ s/^.*[\\\/]//;

# full file path to upload directory (must include filename)
my $localfile = &quot;/home/sulfericacid/public_html/amy/files/$filename&quot;;

# full url to upload directory (cannot include filename or an end slash /)
my $url = &quot;
my $type = uploadInfo($remotefile)->{'Content-Type'};
unless ( $type eq 'image/pjpeg' || $type eq 'image/gif' || $type eq 'image/bmp') {
print &quot;Wrong! This is not a supported file type.&quot;;
exit;
}

# open a new file and transfer bit by bit from what's in the buffer
open( SAVED, &quot;>>$localfile&quot; ); # || die $!;
while ( $bytesread = read( $remotefile, $buffer, 1024 ) ) {
print SAVED $buffer;
}
close SAVED;

chmod $mode, &quot;$localfile&quot;; # or die &quot;can't chmod: $!&quot;;

# required since module was not preinstalled on server
use lib &quot;/home/sulfericacid/public_html/lib/&quot;;
use Image::Info qw(image_info dim);

# assigning info to a filename (better be an image)
my $info =
image_info(&quot;$localfile&quot;);
# if for any reason we can't open the file, this error trap should pick it up
if ( my $error = $info->{error} ) {
#die &quot;Can't parse image info: $error\n&quot;;
}
# unommit next line if you want to use/post the image's color
#my $color = $info->{color_type};

# declaring the width and heighth of your image
my ( $w, $h ) = dim($info);

print &quot;<br>&quot;;
print &quot;<b>File:</b><br>&quot;;
print
qq(File was uploaded to <a href=&quot;$url\/$filename&quot;>$url\/$filename</a><br>);
print
qq(&lt;p style =\&quot;background:url\($url\/$filename\)\;width:$w\;height:$h\;\&quot;&gt;);
print &quot;<br>&quot;;
}


&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
You seem to be calling &dirty_work, and defining $field, but don't pass it?

Try something like;

&dirty_work($field);


.. and in the dirty_work routine, grab this option with;

my $field = $_[0];

...or

my $field = shift;

Remember, a 'my' variable limits the variable scope the the routine it was created in. If you used the following header for your script, it would have picked this up for you;

Code:
#!/usr/bin/perl

use strict;

Cheers

Andy
 
This is the first time I had to use subroutines, that's why I didn't know I had to pass things to them. But that makes me wonder, why would

my $num;

if (param('upload1') {
$num++;
&dirty_worl;
}

work when my example above didn't? I got it to work now, thanks for your help!

&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
>>>work when my example above didn't?<<<

Because you are definding $num OUTSIDE of the routine.. so anything saved to it thereafter (in a routine or not) can be access by any other part of the script.

Glad you got it working :)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top