Forum Moderators: coopster
<?php
$securitylevel=$_COOKIE["securitylevel"];
if ($securitylevel != "admin") {
echo "You cannot access this resource, because are not logged in. Please <a href='../login.php'>log in</a> first.";
}
elseif ($securitylevel=="admin") {
if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/png"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error - There is a problem with the image. " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists($_FILES["file"]["name"]))
{
echo "Error - The image you are trying to upload already exists on the server(" . $_FILES["file"]["name"] . ") " . $FILES["file"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
echo "Image sucessfully saved to server as: " . $_FILES["file"]["name"] . "<br />";
}
}
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect to database: ' . mysql_error() . "<br />");
}
mysql_select_db("dbname", $con);
$name=$_FILES['file']['name'];
$size=$_FILES['file']['size'];
$kb = 1024;
$newsize = round($size / $kb);
$date=date("Y-m-d H:i:s");
$sql = mysql_query("INSERT INTO `images` (name, size, date) VALUES ('$name', '$newsize', '$date')");
if(!$sql) {
echo "Error adding image to database" . mysql_error() . "<br />";
}
mysql_close($con);
echo "<br /><a href='index.php'>Return to Image Manager</a>";
}
else
{
echo "Invalid file - must be a GIF, JPEG, or PNG image.";
}
}
?>
Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. (manual [php.net]
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued. manual [php.net]