MTM2.com

A forum for mtm2 discussion
FAQ :: Search :: Members :: Groups :: Register
Login
It is currently Thu Mar 28, 2024 11:49 am



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Image Upload Script
PostPosted: Sun Apr 04, 2004 12:45 am 
Glow Ball
User avatar

Joined: Tue Feb 02, 1999 7:00 pm
Posts: 19
People always want this, so here's something I came up with that covers everything in a safe and friendly way. It has been tested.

Code:

#!/usr/bin/perl
# ------------------------
# Set these five variables
# ------------------------

# the name of this file/page (CHMOD 755 or 777)
$uploader = "upload.cgi" ;               

# the server path where you want the files uploaded (no trailing slash)
$path  = '/home/user/public_html/images' ;

# the http link to where the files are uploaded  (no trailing slash)
$url   = 'http://domain.com/~user/images' ;

# minimum allowed file size in kbs
$minsize  = "10"  ;

# maximum allowed file size in kbs
$maxsize  = "200" ;


# ------------------------------------------------------------ #
# --------------- Do Not Edit Below This Line ---------------- #
# ------------------------------------------------------------ #
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
print "Content-type: text/html\n\n";
# ----------------------------------- #

$HEAD = <<EOM;
<html>
  <head>
    <title>Image Uploader</title>
  </head>
  <body>
    Image Uploader
    <font face="courier new">
      <pre>
EOM

$FOOT = <<EOM;
      </pre>
    </font>
  </body>
</html>
EOM

# ----------------------------------- #
$upload = param('upload');
if ($upload eq 'yes') {
  &page2();
}
else {
  &page1();
}
# ----------------------------------- #

sub page1 {
print <<EOM;
  $HEAD
    <FORM METHOD="POST" ACTION="$uploader" ENCTYPE="multipart/form-data" >
      <input type="file" name="pic" size="48">
      <input type="hidden" name="upload" value='yes'>
      <input type="submit" value='Upload!'>
    </FORM>
  $FOOT
EOM
exit;
}

# ----------------------------------- #
sub page2 {
$pic = param('pic');
if ( (!defined $pic) || (!$pic) ) {
  &page1();
}
elsif ((lc($pic) !~ /(\.jpg)+$/) && (lc($pic) !~ /(\.gif)+$/)) {
  &at_oops('JPG or GIF only');
}
else {
  $basename = &prep($pic);
  # ---
  open (OUTFILE, ">$path/$basename") || die("Could not open file!") ;
  binmode OUTFILE;
  while ($bytes = read($pic,$buffer,1024)) {
  $size += $bytes;
  print OUTFILE $buffer;
  }
  close(OUTFILE);
  # ---
  $size = sprintf("%d", $size / 1023 );
  if ( (!defined $size) || (!$size) ) {
    unlink("$path/$basename");
    &at_oops('No file size!');
    $basename = undef ;
  }
  elsif ( ((stat "$path/$basename")[7] < $minsize) || ($size > $maxsize) ) {
    unlink("$path/$basename");
    &at_oops('Improper file size!');
    $basename = undef ;
  }
  else {
    $success = 1;
  }
  # ---
}

if ($success) {
print <<EOM;
  $HEAD
    image : $pic
    size  : ${size}k
    link  : <a href="$url/$basename">$url/$basename</a>
  $FOOT
EOM
exit;
}
else {
  &at_oops('No upload');
}
}


# ----------------------------------- #
sub at_oops {
print <<EOM;
  $HEAD
    $_[0]
  $FOOT
EOM
exit;
}

sub prep {
    $_ = $_[0];

    s/\w://;
    s/([^\/\\]+)$//;
    $_ = $1;
    s/\.\.+//g;
    s/\s+//g;

    s/À|Á|Â|Ã|Ä|Å|Æ|à|á|â|ã|ä|å|æ/a/g;
    s/È|É|Ê|Ë|è|é|ê|ë/e/g;
    s/Ì|Í|Î|Ï|ì|í|î|ï/i/g;
    s/Ò|Ó|Ô|Õ|Ö|Ø|ð|ò|ó|ô|õ|ö|ø/o/g;
    s/Ù|Ú|Û|Ü|ù|ú|û|ü/u/g;
    s/¥|Ý|ý|ÿ/y/g;
    s/_//g;
    s/ß/b/g;
    s/Ç|ç/c/g;
    s/Ð/d/g;
    s/Ñ|ñ/n/g;
    s/Þ|þ/p/g;
    s/\$/s/g;

    s/(\.\w{3})$//;
    $extension = $1;
    s/\W//g;
    $_ .= $extension;

    $returnvalue = lc($_);
}

# EOF





I hope the forum posts it properly. You should be able to copy and paste and just use it.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 04, 2004 12:51 am 
Member

Joined: Sat Feb 26, 2000 2:01 pm
Posts: 621
Location: Mississippi , USA
Coolness.. thanks :)

_________________
<p><font color="#990000">"I regret not what I've done, but what I didn't do."</font></p>
<p><a href="http://brandonbatson.com"><font color="#000066"><b><font color="#006666">http://brandonbatson.com
</font></b></font></a> </p>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 12:29 am 
Member

Joined: Sat Feb 26, 2000 2:01 pm
Posts: 621
Location: Mississippi , USA
*bump* Hey.. I found it.

_________________
<p><font color="#990000">"I regret not what I've done, but what I didn't do."</font></p>
<p><a href="http://brandonbatson.com"><font color="#000066"><b><font color="#006666">http://brandonbatson.com
</font></b></font></a> </p>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 4:08 pm 
Member
User avatar

Joined: Mon Jul 02, 2001 2:01 pm
Posts: 1399
Location: Lost in Translation
lol

Should save this though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group