Forum Moderators: coopster & phranque

Message Too Old, No Replies

admin form for creating a linked list of files

to select which files can be listed on user 's page

         

wizpl

6:27 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



Do you know a script that does this -

first part: list all media files from a directory and print them to html page creating a form, where each file name gets its checkbox element. (I could find a script tree.pl, that makes a linked list of files with file sizes etc.)

second part: using the form obove, I'd like to be able to type a name ($name) in a text field, select some media files from the list (using checkboxes), optionally add a $linkname and $comment to each file and hit save button, to save a simple text file with a name: $name.txt, containing a list of all selected files. where each element has a form of something like:
<li><a href:"path to file">$name - $comment</a></li>

Could anyone help me with this. I could also donate creating such a script if it doesn't exist already.

KevinADC

7:56 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



I don't know of any script that does that exact thing. You will have to write one or have one written for you. Do you have any code started yet?

wizpl

9:01 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



so far I have edited the script below to output the list of files how I need it:

<edit>wow, that was too much code -- please see Forum Charter [webmasterworld.com] and try to use specifics -- Thanks, coopster</edit>

[edited by: coopster at 9:54 pm (utc) on Sep. 28, 2005]
[edit reason] removed code [/edit]

KevinADC

9:27 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



yikes, thats a lot of code. Any specific areas you need help with?

wizpl

11:22 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



oh, I am sorry, this script works fine, so I am not asking to debug it, I addmit it was an excessive quote...

I suppose that the second part should be much easier, as the input for this script would already be precisely formated by the first one. I don't feel capable of writing a script on my own, all I can do for now is mostly carefull editing, so I need someone to either direct me to a script that may be adapted for this task, or tell me how much work is it to program it and how much is it worth.

coopster

12:44 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Certainly sounds like custom code to me. It shouldn't be too terribly difficult to tackle though. Why not give it a shot?

  1. Start with the HTML form as you see it after the first script has generated the checkbox listing. Of course, you will need to add the additional form elements you have requested here.
  2. Create your processing script, the code which will handle the next step in your form submission.
  3. Create the final display, the output as it would appear after a successful submission.

Now all that is left is to program step 2. Take the form data collected during step 1 and validate it and manipulate it into the display in step 3. Step 2 will literally write out the file being used in step 3.

wizpl

12:40 am on Sep 30, 2005 (gmt 0)

10+ Year Member



well this is my very first script in my life so don't lough. I am almost there. It displays and creates a file with a list like:

Blind_virgin.mp3=selected
Heaven.mp3=selected
Judyta.mp3=selected
Julia.mp3=selected
Kawdor.mp3=selected
Kurhan.mp3=selected
MKB_Orch2.mp3=selected
Statique.mp3=selected
Suncloud.mp3=selected
Vocara.mp3=selected

of course I need to get rid of those "=selected" parts, which are remains of the form's selection boxes, but I dont know how. I tried the "s/'=selected'//g;" line but it does not work.


#!/usr/bin/perl
require "cgi-lib.pl";
&ReadParse(*input);

# save file
$file = "$input{'userlist'}.inc";
open(FILE, ">$file") ¦¦ die("n$file: $!nn");

shift @input; # delete file name from array
shift @input; # delete button name from array

# Print the item to webpage
print &PrintHeader;
foreach $musicclip (@input)
{ print "$musicclip <br>\n"; }

# Print the item
foreach $musicclip (@input)
{ print FILE "$musicclip\n"; }
s/'=selected'//g; # delete =selected

close(FILE);

KevinADC

2:14 am on Sep 30, 2005 (gmt 0)

10+ Year Member



You want to stop and reconsider your approach. You are using an old module to get your form data:

#!/usr/bin/perl
require "cgi-lib.pl";
&ReadParse(*input);

you will be better off using:

#!/usr/bin/perl
use CGI qw/:standard/;

you then access the form inputs using the param function:

my $foo = param('bar');

where 'bar' is the name of a form field.

Here is a link to the CGI docs:

[perldoc.perl.org...]

the module you are using (cgi-lib.pl) is pre perl 5, and should no longer be used whenever possible.

wizpl

7:08 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



It works now, thanks. Now I only need to learn how to write a file to other directory :)

Here's my script. It writes a file which is ready to be included through ssi:


#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

my @clips = param('mp3');
my @name = param('userlist');
my @title = param('title');

# Print list to webpage
print header;
{ print "<h3>@title</h3>\n"; }
foreach my $mp3 (@clips)
{ print "<a href=\"/members/media/$mp3\" class=\"light\">$mp3</a><br>\n"; }

# save file
$file = "@name.inc";
open(FILE, ">$file") ¦¦ die("n$file: $!nn");

# Print the item
{ print FILE "<h3>@title</h3>\n"; }
foreach my $mp3 (@clips)
{ print FILE "<a href=\"/members/media/$mp3\" class=\"light\"><b>$mp3</b></a><br>\n"; }

close(FILE);

KevinADC

10:07 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I think the best way is to use chdir() to the other directory then write the file, but you can also just add the directory path to the open function:

open(FH,">path/to/$file") or die "$!";

this looks funny:

$file = "@name.inc";

@name is an array and will be expanded inside of double-quotes. Are you sure that's what you want?

wizpl

10:58 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



Thanks Kevin,

yes the file name based on form text field input and it all works now.

wizpl

11:42 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I tried to improve my script adding file sizes to the list of files that it outputs. I used hidden form element to pass the file size (which I have already in a form from the tree.pl script.)

This is my new version, but of course it does not work.
It spits out in one line: "<a href="/members/media/Blind_virgin.mp3" class="light"><b>Blind_virgin.mp3</b></a> 4857kB 7748kB 15275kB 6612kB 8973kB 7051kB 4550kB 7463kB 6169kB 13846kB<br>"

and what I need is only the corresponding file size, not the whole array.

So is there a way I could pass two values from two fields in one line of code?


#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

my @clips = param('mp3');
my @size = param('howbig');
my @name = param('userlist');
my @title = param('title');

# Print list to webpage
print header;
{ print "<h3>@title</h3>\n"; }
foreach my $mp3 (@clips)
{ print "<a href=\"/members/media/$mp3\" class=\"light\">$mp3</a><br>\n"; }

# save file
$file = "/home/members/templates/@name.inc";
open(FILE, ">$file") ¦¦ die("n$file: $!nn");

# Print the item
{ print FILE "<h3>@title</h3>\n"; }
foreach my $mp3 (@clips)
{ print FILE "<a href=\"/members/media/$mp3\" class=\"light\"><b>$mp3</b></a> @size<br>\n"; }

close(FILE);

KevinADC

12:27 am on Oct 1, 2005 (gmt 0)

10+ Year Member



here you want to use indexing instead of a foreach loop:

you have:

foreach my $mp3 (@clips) {
print FILE "<a href=\"/members/media/$mp3\" class=\"light\"><b>$mp3</b></a> @size<br>\n";
}

try this:

for my $i (0 .. $#clips) {
print FILE "<a href=\"/members/media/$clips[$i]\" class=\"light\"><b>$clips[$i]</b></a> $size[$i]<br>\n";
}

KevinADC

12:30 am on Oct 1, 2005 (gmt 0)

10+ Year Member



allowing filenames based on form input is considered dangerous. Make sure you validate the filenames before blindly passing them to your script. It's a way people crack into websites using unsafe scripts.

KevinADC

5:27 am on Oct 1, 2005 (gmt 0)

10+ Year Member



if you want to avoid escaping all of the double-quotes in a double-quoted string use the qq operator:

for my $i (0 .. $#clips) {
print FILE qq~<a href="/members/media/$clips[$i]" class="light"><b>$clips[$i]</b></a> $size[$i]<br>\n~;
}

the tilde "~" is just a delimiter you can use others if you prefer

qq!the string!;
qq#the string#;
etc
etc
etc

wizpl

10:27 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



Thanks Kevin, all this worked, but it seem like I failed somewhere else. I forgot that $size list has more elements, because my form outputs only selected checkboxes but all hidden fields where I stored size values. So I have change the form structure and have this pattern now:


<input name="mp3"
type="checkbox"
value="filename.mp3"
class="selectbox">
<a href="filename.mp3">filename.mp3</a>(4857kB)

<input type="hidden"
name="filename.mp3"
value="4857kB">

so I suppose that "foreach" method should work now:

Foreach "mp3" name, print its value (filename.mp3) and use this value as a name and print its value which is size.

Does it make any sense for you?

BTW file saving, this form will be accessible only from admin area (which means only me) so hopefully it's going to be safe there.

KevinADC

12:05 am on Oct 2, 2005 (gmt 0)

10+ Year Member



it will work like I showed you (using indexing) if @clips and @size are in proper order in relation to each other: first element of @clips relates to first element of @size, etc etc etc.

You have another alternative:

<input name="mp3"
type="checkbox"
value="filename.mp3::4857kB"
class="selectbox">
<a href="filename.mp3">filename.mp3</a>(4857kB)

now the values of @mp3 can be split on the :: and used to generate you links.

foreach my $mp3 (@mp3) {
my ($file,$size) = split(/::/,$mp3);
print FILE qq~<a href="/members/media/$mp3" class="light"><b>$mp3</b></a> $size<br>\n";
}

:: is just an arbitrary delimiter I picked, you could use something different.

wizpl

12:53 am on Oct 3, 2005 (gmt 0)

10+ Year Member



Kevin, thank you so much. Now the script is up and running. You've really opened my eyes to perl :)
I would love to let you listen to my music in return (this is what I do), but I don't want to spam with a link to my page. Let me know if you want the link by email.


foreach my $mp3 (@clips) {
my ($file,$size) = split(/::/,$mp3);
print FILE qq#<a href="/members/media/$file" class="light"><b>$file</b></a>&nbsp;&nbsp;$size<br>\n#;
}

KevinADC

4:25 am on Oct 3, 2005 (gmt 0)

10+ Year Member



cool! Send me the url via sticky mail:

[webmasterworld.com...]