I have created a basic image upload that will place the image into my database. It currently works, but I'm having some troubles with the error checking portion. When I run the program everything works correctly except when I try and put to big of file or the wrong file type. Then it inputs everything except imgdata, filesize and filetype in the database. I'm not sure why it skips my error code portion.
Here is my code:
<?php
// code that will be executed if the form has been submitted:
if (isset($_POST['submit'])) {
//Initialize error array
$errors = array();
//check for a order number
if (empty($_POST['form_description'])) {
$errors[] = 'You forgot the image description.';
}
if (($_FILES['form_data']['size']) > 1000000)
{
$errors[] = 'Image to large, please reduce and try again.';
}
if (($_FILES['form_data']['type']) == "image/jpeg")
{
$errors[] = 'Image needs to be a JPG, please convert and try again.';
}
//Check for Total errors
if(empty($errors)) {
// connect to the database
include 'config_ada.php';
/*** create a new mysqli object with default database***/
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die ("Unable to connect");
$data = addslashes(fread(fopen($_FILES['form_data']['tmp_name'], "r"), $_FILES['form_data']['size']));
// sql to INSERT a new record
$sql = "INSERT INTO pix (title, imgdata, filename, filesize, filetype)
VALUES ('$_POST[form_description]', '$data','".$_FILES['form_data']['name']."','".$_FILES['form_data']['size']."','".$_FILES['form_data']['type']."')";
//excute query
$result = mysqli_query($connection, $sql) or die ("Error in query: $sql. ".mysqli_error());
//$id= mysqli_insert_id();
print "<p>This file has the following Database: <b>$_POST[form_description]</b>";
print "<p><a href=store.php>Home</a>";
// close connection
mysqli_close($connection);
} else {
echo '<html>';
echo '<head>';
echo '<title>Project Database</title>';
echo '</head>';
echo '<div align=center>';
//report the errors
echo '<br><h1><font color=red>Error!</font></h1>The following error(s) occurred: <br /><p>';
foreach ($errors as $msg) {
//print the errors message
echo "- $msg<br />\n";
}
echo '<p><p>Please go back and try again</p></p>';
}
} else {
// else show the form to submit new data:
?>
<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>
<form method="post" action="store.php" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data">
<p><input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
</BODY>
</HTML>
Thanks for any help that you can provide.
A