Form driving me INSANE! :-o

HI everyone, ive spent all day trying to get my form working. I have created a very simple form but I cannot get the ‘file upload’ to upload any files, The form submits the other info to me to my email address but no uploads come my way … please help! I dont need the images sent to me stored anywhere, a simple email attachment is what I need.

My form can be found at https://www.famestreet.com/contact/contacttest.htm and the php page is at https://www.famestreet.com/forms/contact.php

Here is the PHP code:

<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "support@famestreet.com";
$email_subject = "conttest";
     
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
     
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['roles']) ||
!isset($_POST['anything'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$ref = $_POST['ref']; // required
$first_name = $_POST['first_name']; // required
$username = $_POST['username']; // required
$profile = $_POST['profile']; // required
$email_from = $_POST['email']; // required
$roles = $_POST['roles']; // not required
$anything = $_POST['anything']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
     
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
    
$email_message .= "Ref: ".clean_string($ref)."\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Username: ".clean_string($username)."\n";
$email_message .= "Profile: ".clean_string($profile)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "roles: ".clean_string($roles)."\n";
$email_message .= "anything: ".clean_string($anything)."\n";
       
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
<?php
}
die();
?>

Does this information shed any light on your problem? https://www.w3schools.com/php/php_file_upload.asp

There doesn’t seem to be any code that starts the upload.

In addition, it is poor form (no pun intended) to put your labels within the text boxes. This page gives some helpful background on this aspect of form design: https://www.sitepoint.com/definitive-guide-form-label-positioning/

I can’t see anywhere in that PHP code where you try to process the uploaded file. Post the code you were having trouble with so people can suggest what might be wrong with it, rather than deleting all the file-related stuff.

Can’t see this, of course, because your server will execute it rather than showing us the source.

Hi, I have no related code within the php file as i didnt know what to put. Im not too technically minded and was hoping someone could provide the lines of code that need adding … please :slight_smile:

Now you know why your image file is not uploading. Go to the link I gave and see if it gets you started.

Thanks for yuor help and the links. Ive tried to get this to work for about 9 hours now and so am ready to quit, i just cannot do it. My php is below and the contact form is at https://www.famestreet.com/contact/contacttest.htm

Im only just learning html and dont know php at all, im simply cutting / pasting / hoping. If anyone can create me a really simple contact form with image upload then I would be very gratefull, thanks everyone.

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "support@famestreet.com";
$email_subject = "conttest";
     
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
     
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['roles']) ||
!isset($_POST['anything'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$ref = $_POST['ref']; // required
$first_name = $_POST['first_name']; // required
$username = $_POST['username']; // required
$profile = $_POST['profile']; // required
$email_from = $_POST['email']; // required
$roles = $_POST['roles']; // not required
$anything = $_POST['anything']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
     
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
    
$email_message .= "Ref: ".clean_string($ref)."\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Username: ".clean_string($username)."\n";
$email_message .= "Profile: ".clean_string($profile)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "roles: ".clean_string($roles)."\n";
$email_message .= "anything: ".clean_string($anything)."\n";
       
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
<?php
}
die();
?>

Why don’t you try to change that?

Try adding these lines to the top of every PHP page because it will show errors and warnings which may be copied and pasted into a Google Search:

<?php 
declare( strict_types=1);
error_reporting(-1);
ini_set( 'display_errors', 'true');

// your script goes here...

The line of code in your html form to upload the file is this:

Select a file to upload: <input type="file" name="selectedfile" /><br><br>

And yet, in your PHP code, to access that field, you are using this (and others):

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

Can you see that the name is different? As you’ve done with your $_POST variables, the name in the html code for the form is the name that you must use in your PHP code.

You need to work through things step by step, get a bit working and then move on to the next bit. The code you’ve put here for working with the uploaded file is quite complex, in that it checks image size, type and so on - initially you could do away with all that and just get the actual file upload working. Then you add a step - maybe check the file size - and when you’ve got that working, move on to the next step. Apart from anything else, you’ll know which part introduced any errors you might have.

I sympathise, but that’s unlikely to happen here. I’m not speaking on behalf of anyone else here, but to me this is a forum where people post a problem and other people help them to fix it. Not where someone posts a requirement and code is written for them.

That might sound harsh and unhelpful, but I’m presuming that you’re going to have to maintain this code in the future, so it’s no use to you if it’s all just code that someone else wrote.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.