Jump to content

Recommended Posts

Hi I thought I had this solved but its still kicking,

 

I need html and php for this site please. www.penaltysolutions.co.uk

 

ALl I want to be able to do is allow people to upload two pdf's, text files or images not exceeding 20 meg in one go which should be emailed to my site email with the other contact us form info... everything works except the file uploads. Currently contact.html page looks like this:

 

 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>Penalty Solutions Uk</title>

<link rel="stylesheet" type="text/css" href="style.css" media="all" />

</head>

<body>

<div id="header">

<div style= "position:absolute; top:270px; left:930px"

<li><img border="0" src="images/contact us today.jpg"</li>

</div>

<div id="logo">

<a href="index.html"><img src="images/logo.jpg" alt="" /></a>

</div>

<ul>

<li><a href="index.html"><span>home</span></a></li>

<li><a href="about.html"><span>about us</span></a></li>

<li><a href="services.html"><span>services</span></a></li>

<li><a href="products.html"><span>info</span></a></li>

<li class="selected"><a href="contact.html"><span>contact us</span></a></li>

</ul>

 

</div>

<div id="body">

<div class="header">

<div>

<ul>

 

<form name="htmlform" method="post" action="html_form_send.php"enctype="multipart/form-data">

<table width="450px">

</tr>

<tr>

<td style= "position:absolute; top:265px; left:280px">

<label for="first_name">First Name *</label>

</td>

<td style= "position:absolute; top:285px; left:280px">

<input type="text" name="first_name" maxlength="50" size="30">

</td>

</tr>

 

<tr>

<td style= "position:absolute; top:313px; left:280px">

<label for="last_name">Last Name *</label>

</td>

<td style= "position:absolute; top:333px; left:280px">

<input type="text" name="last_name" maxlength="50" size="30">

</td>

</tr>

<tr>

<td style= "position:absolute; top:356px; left:280px">

<label for="email">Email Address *</label>

</td>

<td style= "position:absolute; top:376px; left:280px">

<input type="text" name="email" maxlength="80" size="30">

</td>

 

</tr>

<tr>

<td style= "position:absolute; top:400px; left:280px">

<label for="comname">Company name </label>

</td>

<td style= "position:absolute; top:420px; left:280px">

<input type="text" name="comname" maxlength="50" size="30">

</td>

</tr>

<tr>

<td style= "position:absolute; top:356px; left:570px">

<label for="comweb">Company website </label>

</td>

<td style= "position:absolute; top:376px; left:570px">

<input type="text" name="comweb" maxlength="50" size="30">

</td>

</tr>

<tr>

<td style= "position:absolute; top:400px; left:570px">

<label for="telephone">Telephone Number *</label>

</td>

<td style= "position:absolute; top:420px; left:570px">

<input type="text" name="telephone" maxlength="30" size="30">

</td>

</tr>

<tr>

<td style= "position:absolute; top:446px; left:280px">

<label for="comments">Description of circumstances *</label>

</td>

<td style= "position:absolute; top:469px; left:280px">

<textarea name="comments" maxlength="1000" cols="68" rows="6"></textarea>

</td>

<td style= "position:absolute; top:272px; left:570px" enctype="multipart/form-data" action="html_form_send.php" method="POST" >

Upload file 1: <input type="file" name="uploaded file" />

</td>

<td style= "position:absolute; top:316px; left:570px" enctype="multipart/form-data"action="html_form_send.php" method="POST" >

Upload file 2: <input type="file" name="uploaded file" />

</td>

</tr>

<tr>

<td style= "position:absolute; top:500px; left:930px"colspan="2" style="text-align:center">

<input type="submit" value="Upload file">

</td>

</tr>

</table>

</form>

 

<div id="body">

<div class="about">

 

</div>

 

</div>

<h2></h2>

</div>

</div>

<h2></h2>

</div>

 

 

</div>

<div id="footer">

<div>

<div>

<h3>Contact us</h3>

<ul>

<li>457-380-1654 - main</li>

<li>PenaltySolutionsUK - Skype</li>

</ul>

</div>

<div>

<h3>General Info</h3>

<ul>

<li><li><a style = "font 16pt arial; color:#818a8c;"; href="products.html"><span>Costs explained</span></a></li></li>

<li><li><a style = "font 16pt arial; color:#818a8c;"; href="products.html"><span>PCN issue statistics</span></a></li></li>

</ul>

</div>

<div>

<h3>Information</h3>

<ul>

<li><a style = "font 16pt arial; color:#818a8c;"; href="about.html"><span>Methodology</span></a></li>

<li><a style = "font 16pt arial; color:#818a8c;"; href="about.html"><span>Terms and conditions</span></a></li>

</ul>

</div>

<div>

<h3>Privacy</h3>

<ul>

<li><a style = "font 16pt arial; color:#818a8c;"; href="contact.html"><span>Privacy policy</span></a></li>

<li><a style = "font 16pt arial; color:#818a8c;"; href="contact.html"><span>Data Protection Act 1998</span></a></li>

</ul>

</div>

<div>

<h3>follow us:</h3>

<a class="facebook" href="http://facebook.com/parkingsolutionsuk" target="_blank">facebook</a>

<a class="twitter" href="http://twitter.com/parkingsolutionsuk" target="_blank">twitter</a>

</div>

</div>

<div>

<p>&copy Copyright 2012. All rights reserved</p>

</div>

</div>

</body>

 

</body>

 

</html>

 

and html_form_send.php looks like this:

 

 

<?php

if(isset($_POST['email'])) {

 

// CHANGE THE TWO LINES BELOW

$email_to = "admin@penaltysolutions.co.uk";

$email_subject = "New Entry";

 

function died($error) {

// your error code can go here

echo "We're sorry, but there are errors found with the form you submitted.<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['last_name']) ||

!isset($_POST['email']) ||

!isset($_POST['telephone']) ||

!isset($_POST['comname']) ||

!isset($_POST['comweb']) ||

!isset($_POST['comments'])){

died('We are sorry, but there appears to be a problem with the form you submitted.');

}

 

$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // not required

$comments = $_POST['comments']; // required

$comweb = $_POST['comweb'];

$comname = $_POST['comname'];

 

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

}

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 .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Telephone: ".clean_string($telephone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";

$email_message .= "Comweb: ".clean_string($comweb)."\n";

$email_message .= "Commname: ".clean_string($comname)."\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);

$target_path = "uploads/";

 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

echo "The file ". basename( $_FILES['uploadedfile']['name']).

" has been uploaded";

} else{

echo "There was an error uploading the file, please try again!";

}

?>

 

<!-- place your own success html below -->

 

<html>

<head>

<meta charset="utf-8" />

<title>Penalty Solutions Uk</title>

<link rel="stylesheet" type="text/css" href="style.css" media="all" />

</head>

<body>

<div id="header">

<div id="logo">

<a href="index.html"><img src="images/logo.jpg" alt="" /></a>

</div>

<ul>

<li><a href="index.html"><span>home</span></a></li>

<li><a href="about.html"><span>about us</span></a></li>

<li><a href="services.html"><span>services</span></a></li>

<li><a href="products.html"><span>products</span></a></li>

<li><a href="contact us1.html"><span>contact us</span></a></li>

</ul>

 

<div>

</div>

 

<DIV style="position: absolute; top:300px; left:450px; width:900px; height:45px">Thank you for contacting us. We will be in touch with you very soon.</DIV>

 

<?php

}

die();

?>

 

Would once again really appreciate some wisdom.

Link to comment
https://forums.phpfreaks.com/topic/270178-upload-file-code-for-contact-us-form/
Share on other sites

basically the name, surname etc process goes through, after you submit you get thank you for submitting reply but in top left corner it states "There was an error uploading the file, please try again!". ANd file is no where on directory or part of the email my admin email gets.

 

its online here www.penaltysolutions.co.uk

 

code tags done below.

Edited by newtotheland
 <form name="htmlform" method="post" action="html_form_send.php"enctype="multipart/form-data">
<table width="450px">
</tr>
<tr>
<td style= "position:absolute; top:265px; left:280px">
 <label for="first_name">First Name *</label>
</td>
<td style= "position:absolute; top:285px; left:280px">
 <input  type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>

<tr>
<td style= "position:absolute; top:313px; left:280px">
 <label for="last_name">Last Name *</label>
</td>
<td style= "position:absolute; top:333px; left:280px">
 <input  type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td style= "position:absolute; top:356px; left:280px">
 <label for="email">Email Address *</label>
</td>
<td style= "position:absolute; top:376px; left:280px">
 <input  type="text" name="email" maxlength="80" size="30">
</td>

</tr>
<tr>
<td style= "position:absolute; top:400px; left:280px">
 <label for="comname">Company name </label>
</td>
<td style= "position:absolute; top:420px; left:280px">
 <input  type="text" name="comname" maxlength="50" size="30">
</td>
</tr>
<tr>
<td style= "position:absolute; top:356px; left:570px">
 <label for="comweb">Company website </label>
</td>
<td style= "position:absolute; top:376px; left:570px">
 <input  type="text" name="comweb" maxlength="50" size="30">
</td>
</tr>
<tr>
<td style= "position:absolute; top:400px; left:570px">
 <label for="telephone">Telephone Number *</label>
</td>
<td style= "position:absolute; top:420px; left:570px">
 <input  type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td style= "position:absolute; top:446px; left:280px">
 <label for="comments">Description of circumstances *</label>
</td>
<td style= "position:absolute; top:469px; left:280px">
 <textarea  name="comments" maxlength="1000" cols="68" rows="6"></textarea>
</td>
<td style= "position:absolute; top:272px; left:570px" enctype="multipart/form-data" action="html_form_send.php" method="POST" >
Upload file 1: <input type="file" name="uploaded file" />
</td>
<td style= "position:absolute; top:316px; left:570px" enctype="multipart/form-data"action="html_form_send.php" method="POST" >
Upload file 2: <input type="file" name="uploaded file" />
</td>
</tr>
<tr>
<td style= "position:absolute; top:500px; left:930px"colspan="2" style="text-align:center">
 <input type="submit" value="Upload file"> 
</td>
</tr>
</table>
</form>

 <?php
if(isset($_POST['email'])) {

// CHANGE THE TWO LINES BELOW
$email_to = "admin@penaltysolutions.co.uk";

       $email_subject = "New Entry";

function died($error) {
// your error code can go here
echo "We're sorry, but there are errors found with the form you submitted.<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['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comname']) ||
!isset($_POST['comweb']) ||
!isset($_POST['comments'])){
died('We are sorry, but there appears to be a problem with the form you submitted.'); 
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$comweb = $_POST['comweb'];
$comname = $_POST['comname']; 

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 if(!preg_match($email_exp,$email_from)) {
  $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 }
$string_exp = "/^[A-Za-z .'-]+$/";
 if(!preg_match($string_exp,$first_name)) {
  $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 }
 if(!preg_match($string_exp,$last_name)) {
  $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
 }
 if(strlen($comments) < 2) {
  $error_message .= 'The Comments you entered do not appear to be valid.<br />';
 }
 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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$email_message .= "Comweb: ".clean_string($comweb)."\n";
$email_message .= "Commname: ".clean_string($comname)."\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);
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
   " has been uploaded";
} else{
   echo "There was an error uploading the file, please try again!";
}
?>

<!-- place your own success html below -->

<html>
<head>
<meta charset="utf-8" />
<title>Penalty Solutions Uk</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
</head>
<body>
<div id="header">
<div id="logo">
<a href="index.html"><img src="images/logo.jpg" alt="" /></a> 
</div> 
<ul>
<li><a href="index.html"><span>home</span></a></li>
<li><a href="about.html"><span>about us</span></a></li>
<li><a href="services.html"><span>services</span></a></li>
<li><a href="products.html"><span>products</span></a></li>
<li><a href="contact us1.html"><span>contact us</span></a></li>
</ul>

</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>

<DIV style="position: absolute; top:300px; left:450px; width:900px; height:45px">Thank you for contacting us. We will be in touch with you very soon.</DIV>

<?php
}
die();
?>

Hi

 

$_FILES['uploadedfile'] is not the name of your upload field. $_FILES['uploaded file'] would be correct.

 

//HTML
<input type="file" name="uploaded file" />

//PHP
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

 

You also have some invalid HTML here, td cant have an action / method / enctype.

 

<td style= "position:absolute; top:272px; left:570px" enctype="multipart/form-data" action="html_form_send.php" method="POST" >

 

I suggest you do a print_r($_FILES) to see what the error is. The error will be in the array if there is one. I imagine it will work if you change the $_FILE key, your looking for a key that doesn't exist according to your HTML.

Edited by berridgeab

Hi Berridgeab, sorry for not responding sooner was away for 3 days, thanks again for your reply which resolved the problem but I need it to work for multiple files. Ive tried numerous methods over the last few days but nothings worked. The code below works well but the problem is the 2 files somehow merge into one during upload and when they get to the server and are either corrupt or only have one of the files info in them. Even though on the server the file has a combination of both names.

 

for example, if I upload filea.pdf and fileb.jpeg the filename on the server becomes filea.fileb.jpeg

 

 

<td style= "position:absolute; top:272px; left:570px"form enctype="multipart/form-data" action="html_form_send.php" method="post">
Upload file 1: <input type="file" name="selectedfile" />
</td>
<td style= "position:absolute; top:316px; left:570px"form enctype="multipart/form-data" action="html_form_send.php" method="post">
Upload file 2: <input type="file" name="selectedfile2" />
</td>

 

and the php to look like this:

 

 

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['selectedfile']['name']) .basename( $_FILES['selectedfile2']['name']);

if(move_uploaded_file($_FILES['selectedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['selectedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
if(move_uploaded_file($_FILES['selectedfile2']['tmp_name'], $target_path)) {
echo "The file ". basename2( $_FILES['selectedfile2']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

 

most probably something minor and to do with basename but cant get final bit worked out. Any idea as to why its merging em? Thanks again

Edited by newtotheland

Because that is what you are telling it to name the file in this line:

 

$target_path = $target_path . basename( $_FILES['selectedfile']['name']) .basename( $_FILES['selectedfile2']['name']);

 

In both file uploads, you are saving to the same path, therefore overwriting the first file upload.

Because that is what you are telling it to name the file in this line:

 

$target_path = $target_path . basename( $_FILES['selectedfile']['name']) .basename( $_FILES['selectedfile2']['name']);

 

In both file uploads, you are saving to the same path, therefore overwriting the first file upload.

 

ah ok that makes sense, any suggestion as to what might work instead?

hi all eventually created second directory on server called uploads2 and created new path? I guess which works. Code below. Happy enough, all I have to do is look in two different folders instead of one which isnt a biggie. Thanks loads for responses all, code below..

 

 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['selectedfile']['name']);

if(move_uploaded_file($_FILES['selectedfile']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['selectedfile']['name']). 
   " has been uploaded"; 
} else{
   echo "There was an error uploading the file, please try again!";
}

$target_path = "uploads2/";

$target_path = $target_path . basename( $_FILES['selectedfile2']['name']);

if(move_uploaded_file($_FILES['selectedfile2']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['selectedfile2']['name']). 
   " has been uploaded";
} else{
   echo "There was an error uploading the file, please try again!";
}

hi all eventually created second directory on server called uploads2 and created new path? I guess which works. Code below. Happy enough, all I have to do is look in two different folders instead of one which isnt a biggie. Thanks loads for responses all, code below..

 

 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['selectedfile']['name']);

if(move_uploaded_file($_FILES['selectedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['selectedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

$target_path = "uploads2/";

$target_path = $target_path . basename( $_FILES['selectedfile2']['name']);

if(move_uploaded_file($_FILES['selectedfile2']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['selectedfile2']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

 

Why do you need 2 directories? Store each file name in its own variable:

 

$target_path = "uploads/";
$fileName1 = $_FILES['selectedfile']['name'];
$fileName2 = $_FILES['selectedfile2']['name'];

$target_path1 = $target_path . $fileName1;
$target_path2 = $target_path . $fileName2;


if(move_uploaded_file($_FILES['selectedfile']['tmp_name'], $target_path1))
{
 echo "The file ". $fileName1 . " has been uploaded";
}
else
{
 echo "There was an error uploading the file, please try again!";
}


if(move_uploaded_file($_FILES['selectedfile2']['tmp_name'], $target_path2))
{
 echo "The file ". $fileName2 ." has been uploaded";
}
else
{
 echo "There was an error uploading the file, please try again!";
}

Edited by AyKay47

Why do you need 2 directories? Store each file name in its own variable:

 

$target_path = "uploads/";
$fileName1 = $_FILES['selectedfile']['name'];
$fileName2 = $_FILES['selectedfile2']['name'];

$target_path1 = $target_path . $fileName1;
$target_path2 = $target_path . $fileName2;


if(move_uploaded_file($_FILES['selectedfile']['tmp_name'], $target_path1))
{
echo "The file ". $fileName1 . " has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}


if(move_uploaded_file($_FILES['selectedfile2']['tmp_name'], $target_path2))
{
echo "The file ". $fileName2 ." has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}

 

legend AyKay that works like a dream man, thanks :)

:):):):):)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.