SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CGI software error when trying to upload images

    Hi there, I was working on a script for uploading images using cgi & perl. I have the syntax below, for some reason when I submit from my html form I get:

    Software error:
    Permission denied at /home/jlisec01/public_html/cgi-bin/upload.cgi line 39.

    the html form is fine, also the permissions are set to 755 for the cgi file.

    Code Perl:
    #!/usr/bin/perl -w
     
    use strict;
    use CGI;
    use CGI::Carp qw ( fatalsToBrowser );
    use File::Basename;
     
    $CGI::POST_MAX = 1024 * 5000;
    my $safe_filename_characters = "a-zA-Z0-9_.-";
    my $upload_dir = "/home/jlisec01/public_html/images";
     
    my $query = new CGI;
    my $filename = $query->param("photo");
    my $email_address = $query->param("email_address");
     
    if ( !$filename )
    {
     print $query->header ( );
     print "There was a problem uploading your photo (try a smaller file).";
     exit;
    }
     
    my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
    $filename = $name . $extension;
    $filename =~ tr/ /_/;
    $filename =~ s/[^$safe_filename_characters]//g;
     
    if ( $filename =~ /^([$safe_filename_characters]+)$/ )
    {
     $filename = $1;
    }
    else
    {
     die "Filename contains invalid characters";
    }
     
    my $upload_filehandle = $query->upload("photo");
     
    open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
    binmode UPLOADFILE;
     
    while ( <$upload_filehandle> )
    {
     print UPLOADFILE;
    }
     
    close UPLOADFILE;
     
    print $query->header ( );
    print <<END_HTML;
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <title>Thanks!</title>
       <style type="text/css">
         img {border: none;}
       </style>
     </head>
     <body>
       <p>Thanks for uploading your photo!</p>
       <p>Your email address: $email_address</p>
       <p>Your photo:</p>
       <p><img src="../images/$filename" alt="Photo" /></p>
       <p><a href="https://crux.baker.edu/~jlisec01/file_upload.html">Return back to page</a></p>
     </body>
    </html>
    END_HTML

    id appreciate any help, thanks.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,271
    Mentioned
    197 Post(s)
    Tagged
    3 Thread(s)
    My Perl is so rusty and stale I'm probably way off base, but I'm wondering if instead of
    Code Perl:
    my $upload_dir = "/home/jlisec01/public_html/images";
    it might be better to have
    Code Perl:
    my $upload_dir = "../images";

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Mitt, user seems to be using this old script from SitePoint: http://articles.sitepoint.com/articl...files-cgi-perl
    (old because it's really a 2004 article with a few big holes updated)

    The recommendation is indeed to use the full path name.

    jlisec:
    open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";

    this is line 39? You made absolutely sure upload.cgi has 755 permissions?

  4. #4
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Mitt, user seems to be using this old script from SitePoint: http://articles.sitepoint.com/articl...files-cgi-perl
    (old because it's really a 2004 article with a few big holes updated)

    The recommendation is indeed to use the full path name.

    jlisec:
    open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";

    this is line 39? You made absolutely sure upload.cgi has 755 permissions?
    yes, 100% sure.

  5. #5
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    What about the folder /images?

    if you make another Perl script with the same 755 permissions and just tell it to open /images, you should get the same permissions error.

    I would add perl's group to the /images folder... I dunno what group your perl belongs to, it might be "perl".

    Code:
    sudo chgrp -R (perl's group) /home/jlisec01/public_html/images


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •