I'm a newbie to PHP 🙂 I've cobbled together an image upload script from a few different scripts to work with my Postnuke site, and I've mostly got it doing what I want except for the one section that is supposed to check the width of the image (about halfway down the Upload Script). There are two main files for this script. If anybody sees right off what is wrong, please tell me 🙂 (sorry about all the code, but I wasn't sure what you might need to see to help me figure this out...)
UPLOAD SCRIPT
<?php
global $lang, $newlang, $adminmail;
$modname = "Members_Photo_Upload";
$extensions = array(".jpg", ".jpeg"); //List extensions you want files uploaded to be
$limit_ext = "yes"; //do you want to limit the extensions of files uploaded
if(!IsSet($mainfile))
{
include ("mainfile.php");
}
if (!$user)
{
include("user.php");
}
if (!eregi("modules.php", $PHP_SELF))
{
die ("You can't access this file directly...");
}
if (isset($newlang))
{
$language = $newlang;
}
elseif (isset($lang))
{
$language = $lang;
}
if(file_exists("modules/$modname/language/$language.php"))
{
include("modules/$modname/language/$language.php");
}
else
{
include("modules/$modname/language/english.php");
}
$index = 0;
// Set server Directory (NO TRAILING SLASH)
$server_dir = "/home/2036110209/www/web/modules/XForum/images/avatar";
// Your email
$youremail=$adminmail;
// Thank you page:
$thankspage="modules.php?op=modload&name=$modname&file=thanks";
// If user uploads a file with the same name as one that exists:
$existspage="modules.php?op=modload&name=$modname&file=exists";
// If user uploads a non-allowed file:
$badfilepage="modules.php?op=modload&name=$modname&file=badfile";
// If user uploads a picture that is too big:
$badsizepage="modules.php?op=modload&name=$modname&file=badsize";
// Email message on or off ? 1 = on, 0 = off
$emailmessages = "0";
global $uploadfile1;
global $uploadfile1_name;
global $upload_dir, $thetime, $user, $designname;
//If they don't specify a file to upload, die.
if (! $uploadfile1_name)
{
echo "No file specified or file outside guidelines.\n <a href=javascript:history.back(-1)>Return to the form</a>";
exit;
}
cookiedecode($user);
$check = $cookie[1];
$result = mysql_query("SELECT uname, email FROM $prefix"._users." WHERE uname='$check'");
list($uname, $email) = mysql_fetch_row($result);
$user = $uname;
if ($user == "")
{ // If no name, then return error.
echo _MEMNAME;
exit;
}
else
{
// Check for valid email address
$x = ereg("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$",$email);
if($x==0)
{
// if no valid email address entered, display no email message
echo _MEMEMAIL;
exit;
}
else
{
if(file_exists("$server_dir/$uploadfile1_name"))
{
Header("Location: $existspage");
}
else
{
$ext = strrchr($uploadfile1_name,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions)))
{
Header("Location: $badfilepage");
}
else
{
$imginfo = GetImageSize($uploadfile1_name);
$userWidth = $imginfo[0];
$userHeight = $imginfo[1];
if ($userWidth > 60)
{
Header("Location: $badsizepage");
}
else
{
// This sets the time and creates a directory to upload to
$startdate = time();
$thetime = date("m-d-Y_H-i-s", $startdate);
// Wherever you have write permission below...
$upload_dir = "$server_dir";
// Gets the temp_name in to a string variable
$uploadtemp1 = $uploadfile1;
// Gets those names into an array to be called later
$original = array("$uploadtemp1");
// Same as above except gets the real file name
$uploadreal1 = $uploadfile1_name;
$file = array("$uploadreal1");
// Copy handler for the above arrays
for($I = 0; $I <=6; $I++)
{
$file[$I] = ereg_replace(" ", "_", $file[$I]);
$file[$I] = ereg_replace("%20", "_", $file[$I]);
$copyfile = "$upload_dir/$file[$I]";
@copy($original[$I], $copyfile);
@unlink($original[$I]);
// This will rename the uploaded file to their username, no matter what file name they use.
// You can change $user.jpg to $user.gif or whatever you want.
//rename ("$upload_dir/$file[$I]", "$upload_dir/$user.jpg");
//return thank you page
Header("Location: $thankspage");
}
}
}
// Set Upload email thanks message for USER
$uptymessage="$user, thank you for uploading your photo.\n\nYour image will automatically be added to your articles.";
// Set Upload email thanks message for OWNER
$ownermessage="Username: $user\nEmail: $email\nDirectory: $upload_dir\nPhoto: $uploadreal1\n";
// Send Email
if ($emailmessages == "1")
{
//mail you to let you know a new uploaded file
//mail("$youremail", "Uploaded photo", "$ownermessage", "From: $email\nX-Mailer: PHP/" . phpversion());
//mail user to thank them
//mail("$email", "Uploaded photo", "$uptymessage", "From: $youremail\nX-Mailer: PHP/" . phpversion());
}
}
}
}
?>
INDEX PAGE
<?php
global $lang, $newlang;
$modname = "Members_Photo_Upload";
if (isset($newlang)) {
$language = $newlang;
} elseif (isset($lang)) {
$language = $lang;
}
if(file_exists("modules/$modname/language/$language.php")) {
include("modules/$modname/language/$language.php");
} else {
include("modules/$modname/language/english.php");
}
$index = 0;
if(!IsSet($mainfile)) { include ("mainfile.php"); }
if (!$user) { include("user.php"); }
if (!eregi("modules.php", $PHP_SELF)) { die ("You can't access this file directly..."); }
include("header.php");
echo "<p>";
OpenTable();
cookiedecode($user);
$check = $cookie[1];
$result = mysql_query("SELECT uname, email FROM $prefix".users." WHERE uname='$check'");
list($uname, $email) = mysql_fetch_row($result);
echo "<form action=\"modules.php\" method=\"post\" ENCTYPE=\"multipart/form-data\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"op\" value=\"modload\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"name\" value=\"$modname\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"file\" value=\"upload\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"10000\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"user\" value=\"$uname\">\n";
echo "<INPUT TYPE=\"hidden\" name=\"email\" value=\"$email\">\n";
echo "<center><table border=\"0\" cellpadding=\"2\">\n";
echo "<tr><td colspan=\"2\" align=\"center\"><font class=\"title\"><b>$sitename ".MEMUP."</b></font></td></tr>\n";
echo "<tr><td>".MEMFILE.":</td><td><input type=\"file\" size=\"40\" name=\"uploadfile1\"></td></tr>\n";
echo "<tr><td>".UUSERNAME.":</td><td>$uname</td></tr>\n";
echo "<tr><td>".EMAIL.":</td><td>$email</td></tr>\n";
echo "<tr><td colspan=\"2\"><input type=\"submit\" value=\"Upload\"> <input type=\"reset\" value=\"Clear\"></td></tr>\n";
echo "</table></center>\n";
echo "</form>\n";
echo "<center>Please upload your photo as: (<b>$cookie[1].jpg</b>) - Size: (<b>width=60 height=60</b>)</center>";
CloseTable();
echo "<br>";
OpenTable();
echo "<center>".MEMUP."</center>";
CloseTable();
include("footer.php");
?>