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

Hello,
I'm downloading an image in perl using image::grab. The image is a weather image for my area. The script works, but will only save the new file if I manually delete the old one.
I can't get perl to delete it's own file from within perl. When I try to manually delete it, I get:
override rw-r--r-- www/admin for 1image.jpg?
and as www/admin I can. I tried changing the file permissions before deleting it in my perl script, but it didn't work:
$imgfile=">/Library/WebServer/Documents/gcdata/".$id."image.jpg"; $cmd1 = "chown me ".$imgfile.""; $cmd2 = "chmod 777 ".$imgfile.""; $cmd3 = "rm ".$imgfile.""; if (-e $file){ system "$cmd1"; system "$cmd2"; system "$cmd3"; }
Do I need to run my web accessible perl script as another user? I can usually pull this off, but it's always been with txt documents, not images.
thanks
tim

Replies are listed 'Best First'.
Re: Kill file that I created, and recreate
by b10m (Vicar) on Feb 10, 2004 at 03:17 UTC
    The script works, but will only save the new file if I manually delete the old one.I can't get perl to delete it's own file from within perl.

    It'd be nice if you supplied us with the error you get when you try, and not to forget, the method you try.

    When I try to manually delete it, I get:
    override rw-r--r-- www/admin for 1image.jpg?

    Assuming that your http daemon runs under the user account "www", it looks like a simple unlink should do the trick. That user has write permissions, and should be able to delete the file it created too. You (as in: the user you try to remove the file with) seem to have enough permissions on the directory, but not on the file, hence the warning.

    and as www/admin I can.

    If you can `su` to user "www" and manually delete it, I see no problems why you can't delete the file (or overwrite it, what you probably want to begin with) from within your script.

    I've never seen Image::Grab before, but try this little snippet and please return us the error message you might get (please note that this is almost taken verbatim from the POD):

    use Image::Grab; $url = "http://wherever/you/snatch/your/image/from.jpg" $pic->url($url); $file = "/Library/WebServer/Documents/gcdata/".$id."image.jpg"; open(IMAGE, ">$image") || die "Can't open $image: $!"; print IMAGE $pic->image; close IMAGE;

    And please check for some weird permissions on "/Library/WebServer/Documents/gcdata/". If, for some reason, you want to alter the permissions the file is saved under, you might want to check out umask too.

    $cmd3 = "rm ".$imgfile."";

    Please note that you included a ">" sign in $imgfile, which is probably nagging you here. Please don't use that in the filename, but in the open statement (as my example points out) and of course, use unlink instead of system("rm ...") ;)

    HTH

    --
    b10m

    All code is usually tested, but rarely trusted.
      That's the code I'm referening <from the POD>
      B10m wrote:
      Please note that you included a ">" sign in $imgfile, which is probabl +y nagging you here.<br>
      That was it. The unlink didn't work before, but it might have had to do with the out of place '>'.
      Many Many thanks.
      Tim
Re: Kill file that I created, and recreate
by Zaxo (Archbishop) on Feb 10, 2004 at 02:48 UTC

    You don't say who owns the image file. If it is placed in the file system by the cgi, it is probably owned by the httpd user. You can delete the file with perl's unlink, but write permission is needed on the parent directory inode, not the file.

    You ordinarily cannot execute chown as it is privileged.

    After Compline,
    Zaxo

Re: Kill file that I created, and recreate
by Ao (Friar) on Feb 10, 2004 at 03:13 UTC
    change your fourth line to $cmd3 = "rm -f ".$imgfile.""; to "force" the delete, a.k.a. to not prompt you for verification.