lex2001 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I'm trying to create an upload script that allows the user to enter a name for a song and then also upload the actual file. I have been succesful in getting a script to upload the actual file and also allow the user to delete uploaded files. Now I have added a field where the user can input the name of the song. So I would like to be able to have the user input the name of the song and upload the file and also be able to delete the name of the song and the file. The only thing I can think of to make this work is to create a hash the stores the file name as the key and the song title as an element. However I am not very good with hashes. I have tried doing this to create the hash:
my %test = qw( @song_titles, @song_files );
This does not work but it was the only thing I could think of to get arrays into a hash. @song_titles is being stored in a txt file and I get the data for @song_files by globbing the relevant directory. Here are the relevant sub routines:
#### # Upload HTML #### sub upload_html { print<<HTML; <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO +-8859-1"> <title>Upload</title> </head> <body bgcolor="#ffffff"> <form action="upload.cgi" Method="post" ENCTYPE="multipart/form-da +ta"> <P>Please choose a file to upload: <INPUT TYPE="FILE" NAME="file"> <BR> Name of Song: <INPUT TYPE="TEXT" NAME="f_name"> <INPUT TYPE="HIDDEN" NAME="page" Value=$page> <br> <br> <INPUT TYPE="submit" NAME="action" VALUE="Submit"> <INPUT TYPE="submit" NAME="action" VALUE="Delete"> </FORM> <p></p> HTML chdir "/Library/WebServer/Documents/userpages" or die "Can't set path +for dir: $!\n"; # make directory for user # first check to see if it exists unless (-e $page) { mkdir("$page", 0777) || die "cannot make directory for $page: $!"; } } #### end of upload html #### # Get File List #### sub get_file_list { # set path chdir "/Library/WebServer/Documents/userpages/$page" or die "Can't set + path for dir: $!\n"; my $url = "/userpages/$page/"; my @files=glob("*.*"); print<<HTML; <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO +-8859-1"> <title>Upload - Delete Files</title> </head> <body bgcolor="#ffffff"> <form action="upload.cgi" Method="post" ENCTYPE="multipart/form-da +ta"> <P>List of Files: <br> <INPUT TYPE="HIDDEN" NAME="page" VALUE="$page"> HTML foreach (@files) { print "<br> Delete this File: $_ <INPUT TYPE=\"checkbox\" NAME=\"files +\" VALUE=\"$_\">\n"; } foreach (@files) { print "<br> View <A HREF=\"$url$_\">$_</a>\n"; } print<<HTML; <br> <br> <INPUT TYPE="submit" NAME="action" VALUE="Remove File( +s)"> </FORM> HTML } ### end of get file list #### # Delete File #### sub delete_file { # set path chdir "/Library/WebServer/Documents/userpages/$page" or die "Can't set + path for dir: $!\n"; $page =~ /^([\w.]+)$/; # The "untainted" file is now in $1 $page = $1; die "Bad filename for value page" unless $page; # check for tainted data my $files = $q->param( "files") || error( $q, "couldn't read File valu +es"); $files =~ /^([\/.\w.]+)$/; # The "untainted" file is now in $1 $files = $1; die "Bad filename" unless $files; print<<HTML; <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO +-8859-1"> <title>Upload - File Deleted</title> </head> <body bgcolor="#ffffff"> <form action="upload.cgi" Method="post" ENCTYPE="multipart/form-da +ta"> <P>File(s) Have Been Deleted: <INPUT TYPE="HIDDEN" NAME="page" VALUE="$page"> <br> HTML foreach ($files){ unlink($_); } print<<HTML; <br> <INPUT TYPE="submit" NAME="action" VALUE="Back To Main +"> </FORM> <p></p> <!-- trying to get dir_files to print here --> HTML }

2001-12-03 Edit by Corion : Added missing </CODE> tag.

Replies are listed 'Best First'.
Re: Adding and Deleting Upload File name and File
by larryk (Friar) on Dec 03, 2001 at 14:34 UTC
    instead of:
    my %test = qw( @song_titles, @song_files );
    Update: this is a typo (credit to blakem) - the qw will not concatenate the lists but treat the two array names as strings. instead try:
    my @test{@song_titles} = @song_files;
    have a look in perldoc perldata and look at the section on slices
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
      Your solution is correct, but the analysis of the original code isn't quite right...
      my %test = qw( @song_titles, @song_files );
      Will actually do something more like:
      my %test = ('@song_titles', '@song_files');
      So, you'll get a hash with a single key.

      It will also toss an error if warnings are on....

      -Blake

        <cheek tongue="1">Your reply is correct, but your example isn't quite right</cheek>

        I missed the qw which would indeed mean that the other '@lists' would be treated as strings but your example will yield a valid hash (no warning) since you have an even number of elements in the list:

        my %test = ( '@song_titles' => '@song_files' );
           larryk                                          
        perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
        Will code for food - looking for work - London - CV
        
Re: Adding and Deleting Upload File name and File
by andye (Curate) on Dec 03, 2001 at 16:42 UTC
    Just a quick tip: instead of
    print "<br> Delete this File: $_ <INPUT TYPE=\"checkbox\" NAME=\"files +\" VALUE=\"$_\">\n";
    you can do
    print qq[<br> Delete this File: $_ <INPUT TYPE="checkbox" NAME="files" + VALUE="$_">\n];

    andy "tip of the day" e.