Image upload script

[code]

<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("imageupload", $con);
//mysql_query ("insert into ");

if(isset($_POST[‘submit’])){

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
$path = $_FILES["file"]["name"];
mysql_query("INSERT INTO imageinfo(title,path) VALUES (‘$_POST[title]’, ‘$path’)");
}
else
{
echo "Invalid file";
}
}
$result = mysql_query(‘SELECT *FROM imageinfo’);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table cellpadding="5" cellspacing="5">
<tr bgcolor="#CCCCCC">
<td>ID</td>
<td>Picture</td>
<td>Title</td>
</tr>
<?php    while ($row = mysql_fetch_array($result)){ ?>
<tr bgcolor="#CCCCCC">
<td><?php echo $row[0]; ?></td>
<td><img src="upload/<?php echo $row[1]; ?>" alt="" /></td>
<td><?php echo $row[2]; ?></td>
</tr>
<?php }?>
</table>
<form action="" method="post" enctype="multipart/form-data">
Title: <input type="text" name="title" id="title" /></br>
Upload: <input type="file" name="file" id="file" /><input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

[/code]

Download: imageupload

Leave a Reply

Your email address will not be published. Required fields are marked *


*