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

I'm writing a script which will display the information (width/height dimensions) of an image using Image::Info. The images I'm gathering info from are uploaded via a web-form and they're given low permissions and are set to 'nobody'. In order for Image::Info to load the images into the buffer to determine it's size I need to set the permissions of the image so it can access it.

How can you set file permissions (r, w, x..) and the owner of it during a file upload using CGI?

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Setting file upload permissions
by Heidegger (Hermit) on Apr 18, 2003 at 06:28 UTC

    The script that handles your image upload has to call the chmod function on the uploaded file.

    See perldoc chmod.

    $cnt = chmod 0755, 'foo', 'bar'; chmod 0755, @executables; $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to # --w----r-T $mode = '0644'; chmod oct($mode), 'foo'; # this is better $mode = 0644; chmod $mode, 'foo'; # this is best
      Ok, thanks for the link. I think I understand how the syntax of chmoding works but I can't seem to implement it into my snippet. The files still upload at a very limited access. Any ideas? Thanks for the link, that was very helpful!
      $mode = 0644; # take form data my $remotefile = param('upload'); # make new variable to prevent overwriting of form data my $filename = $remotefile; # remove all directories in the file name path $filename =~ s/^.*[\\\/]//; # full file path to upload directory (must include filename) my $localfile = "/home/sulfericacid/public_html/upload/files/$filename +"; # open a new file and transfer bit by bit from what's in the buffer open (SAVED,">>$localfile") || die $!; while ($bytesread=read($remotefile,$buffer,1024)) { chmod $mode, '$filename'; print SAVED $buffer; } close SAVED;


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        You should call the chmod function after file write to the disk:

        # open a new file and transfer bit by bit from what's in the buffer open (SAVED,">>$localfile") || die $!; while ($bytesread=read($remotefile,$buffer,1024)) { print SAVED $buffer; } close SAVED; chmod $mode, '$filename';

        Hope this should work ;-)

Re: Setting file upload permissions
by Abigail-II (Bishop) on Apr 18, 2003 at 08:50 UTC
    To change the owner of a file, you'd call the chown function. But you have to be the owner of the file, or the superuser to be able to change the owner. Furthermore, sane OSses don't allow you to "give away" files, so only the superuser can change ownership.

    Abigail

Re: Setting file upload permissions
by Heidegger (Hermit) on Apr 18, 2003 at 07:32 UTC

    Print some diagnostic information to the browser.

    How about making the system 'ls -l' call on the file uploaded? Call this function just after the file upload and after the chmod() call. Print this info to the browser.

    Also, type 'umask' command in your Unix command prompt. This will tell you the default file permissions assigned to your user ID. You might also choose to play with the umask command a bit too.