Form not displaying error output

The subject of this may be confusing so I will elaborate, below is my latest attempt at code to upload an image to a folder, I know this may seem like an easy task for some but I have additional functions to add after I get the basics down. Anyway, this version of the code appears to work flawlessly as far as uploading the image to the folder my only problem is error reporting, if the image is uploaded to the folder with no errors then after the upload is complete a line of text should display in green saying “Upload Completed Successfully!” and that doesn’t happen. I know the image is uploaded because I have the upload folder open on another monitor and I can see as the image pops on the screen as it is uploaded to the folder. I’m sure, as usual, I’ve left out some punctuation or I need an additional if (empty($errors)) before the $errrors[]=

Here’s the code
[php]

Untitled 2 Image: <?php // After the form loads, if the submit button is pressed if (isset ($_POST['submit'])) { $errors=array(); //declaration of an array which we will us to display erros $image=$_FILES['image']['name'];// taking the file name and storing in variable $file_tmp=$_FILES['image']['tmp_name'];// this is a temporary variable // now if the image name is empty... if (empty($image)) { $errors[]="Please Enter All Fields";// showing the error as an array } if (empty($errors))// now if the error array is empty this means we have no errors and all things are ok { // Here is where we upload a copy of the image to our folder 'uploads/images/' move_uploaded_file ($file_tmp, 'uploads/images/'.$image); //now we show the errors in a foreach loop, if no errros then 'Upload Completed Successfully' should display in GREEN! $errors[]="Upload Completed Successfully!"; } else { foreach ($errors as $err) { echo "$err"; } } } ?> [/php]

Because you are using else on if (empty($errors)) - that means your foreach loop will not be executed on success.

Do you really need the errors array?

[php]

<?php // After the form loads, if the submit button is pressed if (isset ($_POST['submit'])) { $image=$_FILES['image']['name'];// taking the file name and storing in variable $file_tmp=$_FILES['image']['tmp_name'];// this is a temporary variable // now if the image name is empty... if (!empty($image)) { // Here is where we upload a copy of the image to our folder 'uploads/images/' move_uploaded_file ($file_tmp, 'uploads/images/'.$image); echo "Upload Completed Successfully!"; } else { // Error echo "Please Enter All Fields"; } } ?>

[/php]

I guess I don’t really need them, it’s more of a learning thing for me. I can read every book on the planet about this but unless I actually code it and test it and see my mistakes or how it works I don’t learn so that would really be the only reason. Sorry don’t mean to be a pain in the butt!

There’s nothing wrong with that. Did you see the problem in your original code? Perhaps this will help:

[php]

<?php // After the form loads, if the submit button is pressed if (isset ($_POST['submit'])) { $errors=array(); //declaration of an array which we will us to display erros $image=$_FILES['image']['name'];// taking the file name and storing in variable $file_tmp=$_FILES['image']['tmp_name'];// this is a temporary variable // now if the image name is empty... if (empty($image)) { $errors[]="Please Enter All Fields";// showing the error as an array } if (empty($errors))// now if the error array is empty this means we have no errors and all things are ok { // Here is where we upload a copy of the image to our folder 'uploads/images/' move_uploaded_file ($file_tmp, 'uploads/images/'.$image); //now we show the errors in a foreach loop, if no errros then 'Upload Completed Successfully' should display in GREEN! $errors[]="Upload Completed Successfully!"; } // use of else here means the foreach will never be executed on success. // remove this condition and move the errors array below, for example: /* else { foreach ($errors as $err) { echo "$err"; } } */ foreach ($errors as $err) { echo "$err"; } } ?>

[/php]

for the life of me Matt I can’t get this code to work, I’ve moved on to one that I have gotten to work!
Thanks for your help.

Sponsor our Newsletter | Privacy Policy | Terms of Service