Hi,
First, sorry for the very messy coding, I wrote it ages ago when I didn't have much idea of neatness of coding
Anyway, can anyone see any way that this can be exploited please?:
First, sorry for the very messy coding, I wrote it ages ago when I didn't have much idea of neatness of coding

Anyway, can anyone see any way that this can be exploited please?:
Code:
<?PHP
error_reporting(0);
function RandString(){
$keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$length = 8;
$resultKey = "";
for ($i=0;$i < $length; $i++)
$resultKey .= substr($keyChars, rand(1, strlen($keyChars) ), 1);
return $resultKey;
}
$rands = RandString();
include "db.php";
if (empty($_FILES['userfile'])) {
echo "There was no file entered or the file you tried to upload was too big. <br><a href='index.php'>Click here</a> to return to the index.";
} else {
$validMimes = array(
'image/png' => '.png',
'image/x-png' => '.png',
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
'image/pjpeg' => '.jpg',
'image/bmp' => '.bmp'
);
if(!array_key_exists($_FILES['userfile']['type'], $validMimes)) {
die('Sorry, but the file type you tried to upload is invalid; only images are allowed.');
}
// Where the file is going to be placed
$target_path = "files/";
// Add the original filename to our target path. Result is "uploads/filename.extension"
$target_path = $target_path . basename( substr($_FILES['userfile']['name'], 0, strrpos($_FILE['userfile']['name'], '.')));
$target_path .= $validMimes[$image['userfile']['type']];
$_FILES['uploadedfile']['tmp_name']; // This is how we will get the temporary file...
$target_path = "files/";
$target_path = $target_path . basename( $rands . _ . $_FILES['userfile']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
header("Location: index.php");
//echo "There was an error uploading the file, please try again!";
}
?>
<?PHP
//-------------------- When the above is finished go onto adding it to SQL --------------------//
$filename = strip_tags(basename( $rands . _ . $_FILES['userfile']['name']));
$size = filesize($target_path);
$size = ($size > 512)?( ($size/1024 > 512) ?sprintf("%.02f MB",($size/1024)/1024) :sprintf("%.02f KB",$size/1024)) :sprintf("%d B",$size);
$realfilename = basename( $_FILES['userfile']['name']);
// When submit is pressed, add it to the SQL database
$sql = sprintf("INSERT INTO Files SET " . "File=%s, " . "IP='$REMOTE_ADDR', " . "Size=%s, " . "RealFileName=%s, " . "Date=NOW()",
quote_smart($filename), quote_smart($size), quote_smart($realfilename));
if (mysql_query($sql)) {
header("Location: index.php");
} else {
echo("<P>Error adding file: " .
mysql_error() . "</P>");
}
}
?>