Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

upload an image then send to email

the mail() function

Moderators: macek, egami, gesf

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>

<?php

//Settings
$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 ='';

if(isset($_POST['submit']))
{
//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
if(empty($errors))
{
//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"];
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],
$upload_folder);
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$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');
}
}
///////////////////////////Functions/////////////////
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
knot929
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Feb 26, 2012 8:59 pm

Re: upload an image then send to email

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

Hello,

I think major error is on line

move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],
$upload_folder);

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). :)
User avatar
elephantecho
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Mar 23, 2011 1:28 am
Location: Pune, India


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.