in reply to Deleting Multiple Files

First off, you should use CGI.pm, then you dont have to manually parse the form input.

You may want to look at the contents of $f{'file'}, in relation to how unlink expects its parameter(s). Here's valid and invalid uses of unlink:
unlink "a", "b", "c"; # valid unlink "a b c"; # invalid
I think that your code generates the invalid version. Heres (stripped down) code using CGI.pm
... use CGI; my $form = CGI->new(); if ( $form->param('action'} eq "delete" ) { my @files_to_delete = $form->param('chosen_files'); unlink join ",", @files_to_delete; } ...
hope this helps.
shemp

if not, please include output from:
print $f{'file'};

Replies are listed 'Best First'.
Re:x2 Deleting Multiple Files
by grinder (Bishop) on Jun 04, 2003 at 21:44 UTC
    unlink join ",", @files_to_delete;

    I don't think that is going to do what you expect it to do. If you have three files named a, b and c, it is literally going to try to delete a file named a,b,c. What you need is simply:

    my $nr = unlink @files_to_delete; if( $nr != scalar @files_to_delete ) { print "uh-oh: helpful error message here"; }

    I would, however, make the script run with taint checks enabled, and carefully untaint the filenames before passing them to unlink. I would also deal with each file one by one anyway:

    for( @files_to_delete ) { # untaint $_ here unlink or print "failed to remove $_: $!\n"; }

    update: looking at the OP more carefully, there are two other points to be made: firstly, you don't need to open a file in order to remove it. If you want to test that it's there you can use the -f operator. But I would just try and delete it and see what happens. Some things you can't test for, you just have to try it and see whether it works.

    Secondly, unlink works in terms of the process' current directory. This might not be where you think it is. To remove the possibility of unexpected surprises, you should really be doing something like unlink "$path_to/$_" in my snippet above, where $path_to contains the name of the the directory /all/the/way/from/the/top.

    _____________________________________________
    Come to YAPC::Europe 2003 in Paris, 23-25 July 2003.

      Ah shoot, you're correct, it should just be
      unlink @files_to_delete;
      brainfart
      shemp
      I seem to be running into a wall converting this over to use CGI. I keep getting an error 500
        We can help you with it if you post it on your scratchpad.

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.