I was using this tutorial http://www.onlinebuff.com/article_step-by-step-to-upload-an-image-and-store-in-database-using-php_40.html to build up the code I have now. I'm also quite new to php so I had problems correcting some of his errors.
I tried to store images in the database and read them from there but what I want to do with them later will not allow me to do so, well it will be very difficult to monitor. The students will upload images onto the website and I'd like to monitor what they add up so that if someone does report the image i can then take it down easily. Therefore I have decided to use the file access route
I'd like to upload the image to the server and shortly after when the path is in the database i'd like to display it in, but right now my main concern is getting the file onto the server and the image path into the database.
When I run the php Code I get the error. I don't know why either.
Here is my php Code :
<?php
$con = mysqli_connect('localhost', 'root', 'root', 'koleesy');
if (mysqli_connect_errno()) // Check connection
{ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$dirpath = dirname(getcwd());
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = $dirpath.$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
$Imageup="INSERT into 'images_tbl' ('images_path','submission_date') VALUES
('".$target_path."','".date("Y-m-d")."')";
if ($con->query($Imageup) === TRUE) {
echo "New record created successfully"; }
else{
echo("Error While uploading image on the server");
}
}
}
?>
Here is the index file where i'm testing it. Just an upload button and choose file.
<html lang="en">
<head>
<title>Uploading Image to Folder Test</title>
</head>
<body>
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>
</body>
</html>
I've looked at other examples but they all look very different. They use public void and stuff. Dunno what that is.
