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

Hi all.

I'm fairly new to perl, althought I have programmed C++ for quite some time now.

I'm working with a CGI that allows a user to upload files to a directory. The upload part works fine. I've written a delete subroutine that works well for all but the first file in the directory.

It won't unlink the filename even though the path and the filename are correct.

Thoughts?

update (broquaint): changed title (was Unlink)

Replies are listed 'Best First'.
Re: Unlink
by chromatic (Archbishop) on Nov 13, 2002 at 00:27 UTC

    What does Perl tell you? Use the $! variable to report errors:

    unlink $file or die "Cannot unlink '$file': $!\n";
Re: Unlink
by dws (Chancellor) on Nov 13, 2002 at 00:30 UTC
    It won't unlink the [first] filename even though the path and the filename are correct. Thoughts?

    You don't mention what Operating System you're uploading files to, and the OS matters. On Win32, you won't be able to delete a file if some process has it open. Is it possible that you've opened the first file for some reason, and have forgotten to close it before trying to unlink it?

Re: Unlink
by mccullmw (Initiate) on Nov 13, 2002 at 01:07 UTC
    $! says that the file or directory does not exist, even though it does.

    I'm working on UNIX, specifically Solaris 5.6.

      Maybe you are not in the directory you think to be. I had this problem some years ago under WinNT with different Webservers, and tried something like the following to find out what is my actual working directory:
      #! /usr/bin/perl -w use strict; use Cwd; print "Content-type: text/html\n\n"; print "Actual dir is: ", cwd;

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Hi,
      Are you sure there is no <emph>unprintable</emph> characters in the file name? I had the same problem and it turned out that, aving got the filename from a DOS file, it has a \r appended to it. It didn't show on the ls output, but was there...

      Hoping it helps...


      Leo TheHobbit
Re: Unlink
by Anonymous Monk on Nov 13, 2002 at 04:47 UTC
    My first thought was that you don't have permission to delete the file, but maybe you should post some code for us to see.

    SpaceAce

Re: Unlink doesn't work on first file
by iburrell (Chaplain) on Nov 13, 2002 at 17:16 UTC
    What uploaded file are you trying to delete? CGI.pm will automatically delete the temporary file it writes the upload into. It usually deletes the file when the filehandle is destroyed. The $PRIVATE_TEMPFILES variable causes it to unlink the file before passing the open handle(I don't think it will do this on Win32).

    How are you getting the file names to delete? Are you reading the directory with opendir/readdir or glob? Does it check that the file exists before trying to delete it?

Re: Unlink doesn't work on first file
by mccullmw (Initiate) on Nov 14, 2002 at 18:37 UTC
    Hey everyone. Thanks for your insight.

    The problem actually wasn't with unlink, but rather it was in the way the program stored the filenames in an array. The program globbed the directory, removed all *.CGI files from the array, and then displayed them in a select box.

    Here's the code that displayed the select box:

    foreach (@view_list){ if ($first){ print "<option value=\"$_\"> $_ \n"; } else{ print "<option value=\"$_\" selected> $_ \n"; $first=1; }
    In that else statement, there was an extra space after the $_ in value, causing the first file in the directory to be stored with an extra space on the end. The program then switched all spaces to %20. The actual filename never had a space so it would never match when unlink was called.

      else{ print "<option value=\"$_\" selected> $_ \n"; $first=1; }
      In that else statement, there was an extra space after the $_ in value,

      print statements such as the one above are more readable (and easier to catch errors on) when you use the qq operator (just pick a delimiter that isn't in your string):

      print qq!<option value="$_" selected> $_ \n!;