dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: perl webpages on the fly

  1. #1
    Senior Member
    Join Date
    Sep 2001
    Posts
    443

    perl webpages on the fly

    I'm trying to make an image uploader using flash, so of course I've got to use a separate html page that I have in a separate frame to allow the browse function/input file type. Anyway this separate page needs to have some variables sent from flash + the variable of the resulting browse feature - so I thought it would be easier to get the flash to invoke a perl script that makes the "browse" window on the fly, and also include the variables from flash.

    This is the PERL i'm using to create the HTML page:

    Code:
    #!/usr/bin/perl
    
    require "subparseform.lib";
    &Parse_Form;
    
    $v1 = $formdata{"v1"};
    $v2 = $formdata{"v2"};
    $v3 = $formdata{"v3"};
    $v4 = $formdata{"v4"};
    
    print "Content-type: text/html\n\n";
    
    print '<html>';
    print '<head>';
    print '<meta http-equiv="Content-Type"';
    print 'content="text/html; charset=iso-8859-1"';
    print '<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">';
    print '<title>Upload HTML</title>';
    
    print '<script language="JavaScript">';
    print '<!--';
    print 'function checkSelection(){';
    print 'if (document.picture_upload.file_upload.value == ""){';
    print 'window.alert("Choose a file to upload.");';
    print 'return false;}';
    print 'else{return true;}';
    print '}';
    print 'function tell_flash_cancel(){';
    print 'parent.main.document.photo_choice.SetVariable("sent_file", "cancel");';
    print '}';
    print '//-->';
    print '</script>';
    
    print '</head>';
    
    print '<body bgcolor="#EFE0E3">';
    
    print '<div align="center"><center>';
    print '<form target="main" action="http://www.spencerwills.co.uk/cgi-bin/upload.pl" method="post" name="picture_upload" onSubmit="return checkSelection()">'; 
    print '<input type="hidden" name="v1" VALUE="'.$v1.'">';
    print '<input type="hidden" name="v2" VALUE="'.$v2.'">';
    print '<input type="hidden" name="v3" VALUE="'.$v3.'">';
    print '<input type="hidden" name="v4" VALUE="'.$v4.'">';
    print '<table border="0" cellpadding="10" cellspacing="0" width="700" bgcolor="#EFE0E3">';
    
    print '    <tr>';
    print '        <td width="40%"><align="center"><font size="3">File to upload:</font></td>';
    print '        <td align="right" colspan="2" width="55%"><input type="file" size="50" name="file_upload"></td>';
    print '    </tr>';
    
    print '    <tr>';
    print '        <td width="20%"><align="center"><font size="3">Caption:</font></td>';
    print '        <td align="center" width="50%"><input type="text" size="40" name="caption_upload"></td>';
    print '        <td align="center" width="12%"><input type="button" value="Cancel" onclick="tell_flash_cancel()"></td>';
    print '        <td align="center" width="12%"><input type="submit" name="Submit" value="Submit"></td>';
    print '    </tr>';
    
    print '</table>';
    print '</form>';
    print '</center></div>';
    print '</body>';
    print '</html>';
    
    exit;
    First problem - it creates the html page with source all in one line

    Second problem - the javascript functions don't work (though they do if I have a simple html page invoked instead)

    Third problem - You'll notice that there's no enctype="multipart/form-data" in the "form" even though I think I'll need it to send an image, it's just that ABSOLUTELY NO data gets sent if its in there

    Fourthly - I've set the target to "main" in the form, hoping that when the PERL has uploaded it could send a message to the original flash file to do something else, unfortunately unlike other times I've used the same method, this time it PRINTS the variable to the frame that the flash is in.

    Any thoughts on these subjects??

    Thanks in advance

    swills

    (Musicman - I know you're out there - help!)

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    well, the enctype seems really missing . you would put it into the <form> tag
    You just out lots of print statements there - no surprise it all goes onto a long line. Use one of
    print <<END;
    <html>
    <head>
    ....
    END
    or
    print "<html>\n";
    print "<head>\n";
    to get line breaks in.

    Do you have a perl upload script to throw the file at - the subparseform does not understand file uploads

    Musicman

    BTW: someone/something must be cheating - I cant believe that frontpage generates perl code ,,, with or without the express modifier

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    443
    Thanks for the tip on print <<END, I'll try it out later when I can upload via a sensible ftp.

    The enctype="multipart/form-data" was originally in the form tag, but I discovered it was the reason that NO data was being sent to the OTHER perl script I have that deals with all the variables plus uploads the file. I was just wondering hether you'd come across this/any reason why it should make this difference?

    As to whether the javascript will now work when all the ", ; and ' have been sorted out - we'll wait and see.

    We'll also have to wait to see if the targetting problem gets sorted.

    And as to the frontpage "leftovers" - I've been caught red handed re-using old scripts! It has been removed.

    Thanks so far!

    swills

    p.s. any easy way to tell windows ftp how to send the data, i.e. binary or asc? My .pl files always get corrupted unless I use a third party ftp program.

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    windows ftp? - the one that runs in the dos box ... just type ascii at the ftp> prompt

    Musicman

  5. #5
    Senior Member
    Join Date
    Sep 2001
    Posts
    443
    OK! Got the HTML script looking good, sorted out the javascript functions "talking" to FLASH, but no upload is happening.

    Here's the perl script that makes the "browse" page:

    Code:
    #!/usr/bin/perl
    
    require "subparseform.lib";
    &Parse_Form;
    
    $v1 = $formdata{"v1"};
    $v2 = $formdata{"v2"};
    $v3 = $formdata{"v3"};
    $v4 = $formdata{"v4"};
    
    print"Content-type: text/plain\n\n";
    
    print <<ENDHTML;
    <html>
    <head>
    <title>Upload HTML</title>
    
    <script language="JavaScript">
    <!--
    
    function checkSelection(){
    if (document.picture_upload.file_upload.value == "")
    {
    window.alert("Choose a file to upload.");
    return false;
    }else{return true;}
    }
    
    function tell_flash_cancel()
    {
    parent.main.document.photo_choice.SetVariable("sent_file", "cancel");
    }
    //-->
    </script>
    
    </head>
    
    <body bgcolor="#EFE0E3">
    
    <div align="center"><center>
    
    <form ENCTYPE="multipart/form-data" method="post" action= "../cgi-bin/upload.pl" name="picture_upload" onSubmit="return checkSelection()">
    <input type="hidden" name="v1" VALUE="$v1">
    <input type="hidden" name="v2" VALUE="$v2">
    <input type="hidden" name="v3" VALUE="$v3">
    <input type="hidden" name="v4" VALUE="$v4">
    <table border="0" cellpadding="10" cellspacing="0" width="700" bgcolor="#EFE0E3">
    
         <tr>
             <td width="40%"><align="center"><font size="3">File to upload:</font></td>
             <td align="right" colspan="2" width="55%"><input type="file" size="50" name="file_upload"></td>
         </tr>
         <tr>
             <td width="20%"><align="center"><font size="3">Caption:</font></td>
             <td align="center" width="50%"><input type="text" size="40" name="caption_upload"></td>
             <td align="center" width="12%"><input type="button" value="Cancel" onclick="tell_flash_cancel()"></td>
             <td align="center" width="12%"><input type="submit" name="Submit" value="Submit"></td>
         </tr>
    
    </table>
    </form>
    </center></div>
    </body>
    </html>
    ENDHTML
    
    exit;
    variables come through no problem, but with the ENCTYPE="multipart/form-data" in this below doesn't work:

    Code:
    #!/usr/bin/perl
    
    require "subparseform.lib";
    &Parse_Form;
    
    $file_upload = $formdata{'file_upload'};
    $caption_upload = $formdata{'caption_upload'};
    $photo_dir = $formdata{'v1'};
    $group_name = $formdata{'v2'};
    $new_dir = $formdata{'v3'};
    $new_group = $formdata{'v4'};
    
    $file_path = $file_upload;
    $file_path =~ s/^.*\\//;
    if($file_path =~ m/^(\w+)\.(\w+)/){
    $photo_name = $1;
    $ext = $2;
    }
    
    use CGI; 
    print "Content-type: perl/html\n\n";
    if ($file_upload ne "") {
    my $fileName = $file_upload; 
    $fileName =~ s!^.*(\\|\/)!!; 
    open (OUTFILE, ">image_temp/$fileName"); 
    while (my $bytesread = read($file_upload, my $buffer, 1024)) { 
    binmode(OUTFILE);
    print OUTFILE $buffer; 
    } 
    close (OUTFILE); 
    }
    }
    
    #################################
    sub loadin_and_sort_data_files{
    
    open READ_FILE,($_[0]) or die "cannot read $!";
    $line = <READ_FILE>;
    chop;
    
    	while ($line ne "") {
    	@pairs = split(/&/, $line);
    	foreach $pair (@pairs) {
    	($key, $value) = split (/=/, $pair);
    		$key =~ tr/+/ /;
    		$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    		$value =~ tr/+/ /;
    		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    		$value =~s/<!--(.|\n)*-->//g;
    	if ($formdata{$key}) {$formdata{$key} .= ", $value";} else {$formdata{$key} = $value;}
    	}
    	$line = <READ_FILE>;
    	}
    close (READ_FILE);
    }
    
    #######make new directories and change available_dirs.txt########
    if($photo_dir eq "new"){
    
    &loadin_and_sort_data_files("../photos/available_dirs.txt");
    
    open WRITE_FILE,(">../photos/available_dirs.txt") or die "cannot write to $!";
    $numb_dirs = $formdata{'numb_dirs'};
    $new_numb = $numb_dirs+1;
    print WRITE_FILE ("numb_dirs=$new_numb\&\n");
    for($i=1;$i<=$numb_dirs;$i++)
    {
    $temp1 = eval("dir$i");
    $temp2 = $formdata{$temp1};
    if($temp2 eq $new_dir){$new_dir = "$new_dir\_copy";}
    print WRITE_FILE ("\&dir$i=$temp2\&\n");
    }
    print WRITE_FILE ("\&dir$new_numb=$new_dir\&\n");
    close (WRITE_FILE);
    
    mkdir "../photos\/$new_dir", 0755;
    mkdir "../photos\/$new_dir\/images", 0755;
    mkdir "../photos\/$new_dir\/thumbnails", 0755;
    
    open MENU,(">../photos\/$new_dir\/photomenu.txt") or die "cannot create $!";
    print MENU ("number_groups=1\&\n");
    print MENU ("\&group1=$new_group\,$photo_name\,empty\,empty\,empty\,empty\,empty\,empty\,empty\,empty\&\n");
    print MENU ("\&caption1=\,$caption_upload\,\&\n");
    close (MENU);
    
    #####  now do something with the saved file
    }######### end of new directory making#####################
    
    ######make a new group in an existing directory#################
    if(($group_name eq "new") && ($photo_dir ne "new")){
    
    &loadin_and_sort_data_files("../photos\/$photo_dir\/photomenu.txt");
    
    open WRITE_FILE,(">../photos\/$photo_dir\/photomenu.txt") or die "cannot write to $!";
    $number_groups = $formdata{'number_groups'};
    $new_numb = $number_groups+1;
    print WRITE_FILE ("number_groups=$new_numb\&\n");
    for($i=1;$i<=$number_groups;$i++)
    {
    $temp1 = eval("group$i");
    $temp2 = $formdata{$temp1};
    $temp3 = eval("caption$i");
    $temp4 = $formdata{$temp3};
    
    print WRITE_FILE ("\&group$i=$temp2\&\n");
    print WRITE_FILE ("\&caption$i=$temp4\&\n");
    }
    print WRITE_FILE ("\&group$new_numb=$new_group\,previous\,$photo_name\,empty\,empty\,empty\,empty\,empty\,empty\,empty\&\n");
    print WRITE_FILE ("\&caption$new_numb=\,\,$caption_upload\,\&\n");
    close (WRITE_FILE);
    
    #####  now do something with the saved file
    }###end of new group in an existing directory################
    
    ##### just change an existing group##################
    if(($group_name ne "new") && ($photo_dir ne "new")){
    
    &loadin_and_sort_data_files("../photos\/$photo_dir\/photomenu.txt");
    
    open WRITE_FILE,(">../photos\/$photo_dir\/photomenu.txt") or die "cannot write to $!";
    $number_groups = $formdata{'number_groups'};
    print WRITE_FILE ("number_groups=$number_groups\&\n");
    
    for($i=1;$i<=$number_groups;$i++)
    {
    $temp1 = eval("group$i");
    $temp2 = $formdata{$temp1};
    $temp3 = eval("caption$i");
    $temp4 = $formdata{$temp3};
    
    if($temp2 =~ m/$group_name\,/){
    $temp2 =~ s/empty/$photo_name/;
    
    if($temp2 =~ m/empty\,empty/){
    }else{$temp2 =~ s/empty/next/;}
    $temp4 = "$temp4$caption_upload\,";
    }
    print WRITE_FILE ("\&group$i=$temp2\&\n");
    print WRITE_FILE ("\&caption$i=$temp4\&\n");
    }
    close (WRITE_FILE);
    #####  now do something with the saved file
    }####end of just changing an existing group##################
    
    print"Content-type: text/plain\n\n";
    print <<ENDHTML;
    <script language="JavaScript">
    <!--###tell flash to get on with it
    parent.main.document.photo_choice.SetVariable("sent_file", "done");
    //-->
    </script>
    ENDHTML
    though if I leave it out and don't have the upload script in it all the variables get changed appropriately.

    Am I doing something stupid?

    Thanks

    swills

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    your script starts with calling parsefor, - this will read (and not understand) the post data that come with an upload.
    Later it seems to use cgi methods too

    Musicman

  7. #7
    Senior Member
    Join Date
    Sep 2001
    Posts
    443
    Thanks Musicman

    For some reason, possibly the fact that you have to specifically write ENCTYPE="multipart/form-data", I thought the two "kinds" of data would be dealt with differently,by different parts of the script.

    So does anyone know what's the usual way to upload a picture and send a "caption" or "comment" along at the same time? If the file is sent as well as the variable for the "comment", how do you distinguish between them?

    I'm having so much fun!

    swills

  8. #8
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    once you use the proper enctype, a file upload with a var would look like this

    Content-type: multipart_form-data;boundary=-----------3456789
    -----------3456789
    Content-disposition: form-data; name="comment"

    blah blah blah
    -----------3456789
    Content-dispostion: form-data; name="image"; filename="my_best_jpeg_ever.jpg"
    Content-type: image/jpeg

    .... image data gose here
    -----------3456789--

    While variables will usually be listed in the order they appear on the html page, you cannot really count on that.
    Therefore, the cgi module (and others that support upload) will read all variables from the post data

    There is a tricky task not handled by this approach: you cannot determine from the variables whether you want to accept the post at all

    Musicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center