upload an image then send to email

the mail() function

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Sun Feb 26, 2012 8:59 pm

upload an image then send to email

Postby knot929 » Sun Feb 26, 2012 9:07 pm

ok so I have a form on my website and when submitted it send me an email with all the inputs. the image is then supposed to be saved to a folder on the server. but nothing is ever saved. im not sure whats wrong. any help would be greatly appreciated. here is the code:

<form action="http://www.drunkbrokehappy.com/send-email-form.php" method="POST" name="email_form_with_php" enctype="multipart/form-data"><label for="title">Title: </label>
<input type="text" name="title" />
<label for="story">Story:</label>
<textarea name="story"></textarea>
<label for="uploaded_file">Upload file:</label>
<input type="file" name="uploaded_file" />
<input type="submit" name="submit" value="Submit" /></form>


$max_allowed_file_size = 50000; // size in KB
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'drunkbrokehappy@gmail.com';//<<-- update this to your email address

$errors ='';

//Get the uploaded file information
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;

///------------Do Validations-------------

if($size_of_uploaded_file > $max_allowed_file_size )
$errors .= "\n Size of file should be less than $max_allowed_file_size";

//send the email
//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
$errors .= '\n error while copying the uploaded file';

//send the email
$title = $_POST['title'];
$story = $_POST['story'];
$from = $your_email;
$subject = "new submission";

$message = "
Title: $title
Story: $story
uploadedfile: $name_of_uploaded_file"
mail($your_email, $subject, $message);

//redirect to 'thank-you page
header('Location: http://www.drunkbrokehappy.com/thankyou');
// Function to validate against any email injection attempts
function IsInjected($str)
$injections = array('(\n+)',
$inject = join('|', $injections);
$inject = "/$inject/i";
return true;
return false;

User avatar
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Mar 23, 2011 1:28 am
Location: Pune, India

Re: upload an image then send to email

Postby elephantecho » Tue Feb 28, 2012 1:05 am


I think major error is on line


at this line $upload_folder should contain [b]image name to be uploaded along with its path[b].

fixing this will upload your file(make sure upload folder permissions are right). :)

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest