Evening All,
I'm working on a script that NotDog kindly helped me with. It uploads a file to my server and the persons name, email and the filename of the uploaded file to my database.
Now it works fine, apart from the error checking which is what I was stuck on before before.
If I don't enter any details in the name or email I get an error message
BUT if I fill in name and email BUT either leave the upload form field empty, or the file uploaded is over 2mb I get no error message when I should. I just can't see why though. I've fiddled and just can't get it to work.
Can anyone please advise?
Thanks
<?php
if (isset($_POST['Submit']))
{
$errors = array();
$byte = "2097152";
$your_name = trim($_POST['your_name']);
$your_email = trim($_POST['your_email']);
// Check for normal errors in name and email
if ($your_name === '')
{
$errors[] = "You forgot to enter your name.";
}
if ($your_email === '')
{
$errors[] = "You forgot to enter your email address.";
}
// Check to see if the uploaded file is empty and greater than the $byte variable set above, if it is bigger or there is nothing do the below
if(empty($_FILES['ufile']))
{
$errors[] = "No file was uploaded.";
}
else if($_FILES['ufile']['size'][0] > $byte)
{
$errors[] = "Your file is too big.";
}
// If errors is equal to zero then set the image details and insert into the database!
if(count($errors) == 0)
{
// set the image details and upload the file
$picunique = md5(uniqid());
$picture = "/home/*******/public_html/images/$picunique".$_FILES['ufile']['name'][0];
move_uploaded_file ($_FILES['ufile']['tmp_name'][0], $picture);
// and insert into the database
$result = mysql_query("Insert into image_test (your_name,your_email,picture) values('$your_name','$your_email','". basename($picture) ."')") or die(mysql_error());
$picture_id=mysql_insert_id();
$error = "Sucess - everything uploaded";
}
else
{
$error = "<span style='color:#c00'>" . implode(' ', $errors) . "</span>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
<h3>Submit your Photo</h3>
<?php echo $error;?>
<fieldset>
<legend>Please fill in the details below: </legend>
<p>
<label for="Name">Your Name:</label>
<input type="text" name="your_name" id="your_name" value="" tabindex="1" />
</p>
<p>
<label for="Eail">Your Email:</label>
<input type="text" name="your_email" id="your_email" value="" tabindex="2" />
</p>
<div style="clear:left;"></div>
<p>
<label for="Main Picture">Upload Image:</label>
<input name="ufile[]" type="file" id="ufile[]" />
</p>
<input name="Submit" type="submit" value="Submit" />
<input name="Reset" type="reset" value="Reset" />
</fieldset>
</form>
</body>
</html>