PHP email form with file upload

not as attachment

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 10114 Views - Last Post: 27 August 2010 - 10:55 PM Rate Topic: -----

#1 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

PHP email form with file upload

Posted 15 August 2010 - 12:00 PM

The goal of the following script is to upload a file on the server, and upon successful upload an email is sent to let me know about it.

here is the form: http://eleganteditions.com/upload.html

Part one works, the image file uploads correctly in all the specified flavors, file sizes and dimensions.

Part two, not so much. Bare with me, i'm a designer, not a coder, so what i did here is a bit of cutting and pasting to put together something functional and EASY based on the few half-a'd assumptions i'm allowed to make.

Ok now scroll all the way down and have a look at my mail function: yep... right THERE! I have put it there because i thought that might get executed alongside the upload... but when i hit the button the upload works but i get no email. Am i going in the right direction here or am i just weeing in the wind?

Thanks in advance,

- Fabio

<?php
// get posted data into local variables
$EmailTo = "info@fabiobasile.com";
$Subject = "A new file was uploaded!";
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email']));
$comments = Trim(stripslashes($_POST['comments']));

// prepare email body text
$Body .= "name: ";
$Body .= $name;
$Body .= "\n";

$Body .= "e-mail: ";
$Body .= $email;
$Body .= "\n";

$Body .= "comments: ";
$Body .= $comments;
$Body .= "\n";

// ==============
// Configuration
// ==============
$uploaddir = "../upload";
// Where you want the files to upload to
//Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, JPG, jpeg, gif, png, pdf";
// These are the allowed extensions of the files that are uploaded
$max_size = "10485760";
// 50000 is the same as 50kb
$max_height = "6000";
// This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "6000";
// This is in pixels - Leave this field empty if you don't want to upload images

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_SIZE\">";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_DIMENSION\">";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_OK\">";
} else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_EXTENSION\">";
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP email form with file upload

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,005
  • Joined: 08-August 08

Re: PHP email form with file upload

Posted 15 August 2010 - 01:25 PM

Try outputting the information you're sending just before you send it:
echo "To: ".$EmailTo." subj: ".$Subject." Body: ". $Body."From: ".$EmailFrom."<br>";


Is the email being built the way you expect?
Was This Post Helpful? 0
  • +
  • -

#3 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 15 August 2010 - 11:37 PM

View PostCTphpnwb, on 15 August 2010 - 12:25 PM, said:

Try outputting the information you're sending just before you send it:
echo "To: ".$EmailTo." subj: ".$Subject." Body: ". $Body."From: ".$EmailFrom."<br>";


Is the email being built the way you expect?


I have replaced my
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
with yours at the same line but i still get no email back, maybe i should place that somewhere else? Or maybe if there is a way to combine the two lines into one line that uploads and send an email at the same time?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: PHP email form with file upload

Posted 16 August 2010 - 03:43 AM

View Postfabiobasile, on 16 August 2010 - 06:37 AM, said:

View PostCTphpnwb, on 15 August 2010 - 12:25 PM, said:

Try outputting the information you're sending just before you send it:
echo "To: ".$EmailTo." subj: ".$Subject." Body: ". $Body."From: ".$EmailFrom."<br>";


Is the email being built the way you expect?


I have replaced my
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
with yours at the same line but i still get no email back,


that line was not intended to send emails, it was intended to show the variables content before calling mail().
Was This Post Helpful? 1
  • +
  • -

#5 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 16 August 2010 - 10:35 AM

Sorry, brain fart... here is the output, it shows all the info i need in the body of the email i expect to get:

To: info@fabiobasile.com subj: A new file was uploaded! Body: name: test e-mail: test comments: test From: 

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,005
  • Joined: 08-August 08

Re: PHP email form with file upload

Posted 16 August 2010 - 10:55 AM

Is there anything in your log file? Mine is called: php_error.log
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: PHP email form with file upload

Posted 16 August 2010 - 11:05 AM

Are you sure your mail is not ending up in a spam folder or something? Some spam filters will assume spam if headers it wants to see are not present. There are PHP mailer libraries that are helpful in getting around these issues.
Was This Post Helpful? 0
  • +
  • -

#8 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 16 August 2010 - 05:48 PM

View PostJackOfAllTrades, on 16 August 2010 - 10:05 AM, said:

Are you sure your mail is not ending up in a spam folder or something? Some spam filters will assume spam if headers it wants to see are not present. There are PHP mailer libraries that are helpful in getting around these issues.


I'd rule that out, i have made sure i am able to receive email from my own address, i have also tried to point it to my other gmail address and another one of my own hosted addresses and zilch...

About the error logs i'll have to look into where php error logs are kept on GoDaddy, i haven't noticed any on the root folder, but maybe the cpanel would allow me to look that up.

oh another thing that makes me rule out any spam filter implication is that the portiion of code for sending email in the file upload script i hacked it from my other email form from the contact page at http://eleganteditio...om/contact.html which works just fine... and that's where i got the idea of trying to combine the two.
Was This Post Helpful? 0
  • +
  • -

#9 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 27 August 2010 - 09:53 AM

LOL I'm starting to hear crickets... anyway i have been trying a number of ways but so far no dice. Upload still works, and now i get email validation, but i think that happens only due to the position of the validation string... umm... i still get no email back thou.

Any ideas?


<?php

// get posted data into local variables
//$EmailTo = "webmaster@eleganteditions.com";
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "info@fabiobasile.com";
$Subject = "A new file was uploaded";
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email']));
$comments = Trim(stripslashes($_POST['comments']));

// validation
$validationOK=true;
if (Trim($name)=="") $validationOK=false;
if (Trim($email)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../ERROR.html\">";
  exit;
}


// prepare email body text
$Body .= "name: ";
$Body .= $name;
$Body .= "\n";

$Body .= "e-mail: ";
$Body .= $email;
$Body .= "\n";

$Body .= "comments: ";
$Body .= $comments;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page 
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_OK\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../ERROR.html\">";
}

//==========
// Upload script
//==========

// ==============
// Configuration
// ==============
$uploaddir = "../upload";
// Where you want the files to upload to
//Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, JPG, jpeg, gif, png, pdf";
// These are the allowed extensions of the files that are uploaded
$max_size = "10485760";
// 50000 is the same as 50kb
$max_height = "6000";
// This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "6000";
// This is in pixels - Leave this field empty if you don't want to upload images

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_SIZE\">";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_DIMENSION\">";
exit;
}
}


// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);

}
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_OK\">";
} else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=../upload_ERROR_EXTENSION\">";
}
?>

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,005
  • Joined: 08-August 08

Re: PHP email form with file upload

Posted 27 August 2010 - 11:47 AM

Try this for your success test:
if ($success){
  file_put_contents("test.txt", "Success");
} else {
  file_put_contents("test.txt", "Fail");
}


What gets written to the file?

This post has been edited by CTphpnwb: 27 August 2010 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#11 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 27 August 2010 - 12:19 PM

  file_put_contents("test.txt", "Success"); 


returns a fatal error and it won't write anything in test.txt
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,005
  • Joined: 08-August 08

Re: PHP email form with file upload

Posted 27 August 2010 - 12:32 PM

View Postfabiobasile, on 27 August 2010 - 02:19 PM, said:

  file_put_contents("test.txt", "Success"); 


returns a fatal error and it won't write anything in test.txt

:ohno:
Sounds like you've got a permissions problem. Does this work as a separate php file?
<?php
file_put_contents("test.txt", "Success");
?>

You might want to change the file name if to something you don't already have in your root directory.
Was This Post Helpful? 0
  • +
  • -

#13 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 27 August 2010 - 12:43 PM

The error string appears for a second, then it skips on the success page for the upload form.
I erased test.txt and tried again and i get the same result...

I think i got a better glimpse this time and it says 'Fatal Error: Call to undefined action etc...' and returns the path to upload.php with error in line 101 which is where 'file_put_contents("test.txt", "Success"); ' is.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,005
  • Joined: 08-August 08

Re: PHP email form with file upload

Posted 27 August 2010 - 01:04 PM

Must be that you're using an older version of php. You can call phpinfo(); to find out your version. If it's less than 5, then file_put_contents won't work. If it's less than 4, neither will mail.
Was This Post Helpful? 0
  • +
  • -

#15 fabiobasile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 15-August 10

Re: PHP email form with file upload

Posted 27 August 2010 - 01:27 PM

i got php 4.x so i guess file_put_contents is not an option...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2