Jump to content

Image upload and email - Simple mistake I'm sure


webecho

Recommended Posts

Hi Guys

 

I'm trying to get clients to:

1. upload an image

2. fill out details

3. submit image and details to me via email

 

1. Uploading the image is fine, they choose it and it displays on the next page

2. They fill in form details

3. then click submit to send to me - I get all details except the image

 

How do I keep the image variable around to insert it into the details form and email it?

 

The image appears to be displayed on the second page, then the variable disappears (is deleted) and isn't available in step 3 to submit as part of the email.

 

I'm sure it's a simple fix - I'm pretty new with php and I've started kicking things so I thought it was time to ask for help.

 

Upload-image-code

 

<?php if($_FILES)
{ 
$img = $_FILES['filename']['img'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg' : $ext = 'jpg'; break;
case 'image/png' : $ext = 'png'; break;
default: $ext = ''; break;
}
if ($ext)
{
$n = "image.$ext";
move_uploaded_file($_FILES['filename']['tmp_name'], $n);
echo "<span class='uploaded-photo'>uploaded image '$img'  as '$n':</span> <br />";
echo "<img src='$n' />";
}
else echo "'$img' is not a supported image file";
}
//else echo "<span class='uploaded-photo'>No image has been uploaded</span>";

?>

 

$n is available to be displayed on page 2 and it works

Page 2 contains the rest of the form, but when I submit the form $n isn't available to be sent with the other data

(i have tried REQUEST $img as well.

Submit form code

<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$pname = $_REQUEST['pname'] ;
$pdescription = $_REQUEST['pdescription'];
$plow = $_REQUEST['plow'] ;
$phigh = $_REQUEST['phigh'] ;
$purl = $_REQUEST['purl'] ;
$ponline = $_REQUEST['ponline'] ;
$n = $_REQUEST['n'];
$img = $_REQUEST['img'];


if (!isset($_REQUEST['email'])) {
    echo "we need your name and email address please" ;
  }

elseif (empty($name)){
echo "please enter your name";
}
elseif (empty($email)){
echo "please enter your email address";
}
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})", "$email")) {
echo"please check your email address - it doesn't appear to be a valid format";
}
else {
    mail( "eamail@address.com", "Subject",
     "$name\n $email\n $phone\n $address\n $pname\n $pdescription\n $plow\n $phigh\n $purl\n $ponline\n $n\n $img", "$email");
   
    header( "Location: ../index.php" );
  }


?>

 

Would really appreciate any help - if you need full code please let me know.

 

Thanks

 

 

@webecho

 

Link to comment
Share on other sites

you can't just send an image in an email like that, you must add it as an attachment... or link to a url.

In that email you're linking to an image via URL, though the URL is relative to the server upload, not the email...

you need the email's image tag to include the full path

<img src='http://www.domain.com/images/upload/image.jpg' />

 

If you want to attach the image, you'll have to read up on some tutorials or use something like php mailer()

Link to comment
Share on other sites

Thanks Joel

 

So I make an 'uploaded-images' folder and put them in there.

Is that happening from the code I've used so far?

how do I add that image file url into the email?

 

Also I guess I have to find a way to name / number the uploads each time (or can the 'new' image.jpg just overwirte the previous image.jpg?

 

Sorry to ask for more, I normally just use php to include headers, footers and common parts of a webpage most of the time.

If I'm being lazy - please tell me to look it up :)

 

Thanks again

 

@webecho

Link to comment
Share on other sites

Yes that is whats happening in the code so far, it is working from a page on the server because you're pointing to the image relative to the page, though once you're trying to view it from outside your server you need to add http://www.yourdomain.com/whateverFolders/image.jpg

You also need to include the <img> HTML tag

//i don't know your domain, so i'm using $_SERVER['server_name'] which returns www.phpfreaks.com if it were run on the php freaks server
$image = "<img src='http://{$_SERVER["SERVER_NAME"]}$n' />";
    mail( "eamail@address.com", "Subject",
     "$name\n $email\n $phone\n $address\n $pname\n $pdescription\n $plow\n $phigh\n $purl\n $ponline\n $image\n $img", "$email");

 

You don't want to overwrite the image, set the image name to have time() at the end of it which is the amount of seconds passed since jan 1 1970... in other words as long as 2 images aren't uploaded in the same second then you won't have any problems.

 

if ($ext)
{
$n = "image".time().".$ext";
move_uploaded_file($_FILES['filename']['tmp_name'], $n);
echo "<span class='uploaded-photo'>uploaded image '$img'  as '$n':</span> <br />";
echo "<img src='$n' />";
}
else echo "'$img' is not a supported image file";
}

Link to comment
Share on other sites

Hi Joel

Thank for the help so far.

 

I've done a little more reading and have managed to change things so the photos get upload to"uploaded-images" and have changed paths so it still displays on screen.

 

 

I'm still not able to send it in the email though

using

$image = "image <img src='http://www.website.com.au/$n' />";

 

all I get through on the email is

 

image <img src='http://www.website.com.au/' />

 

There is nothing after the domain name?

 

Have I put it in the wrong place?

 

Complete sendmail code again

<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$pname = $_REQUEST['pname'] ;
$pdescription = $_REQUEST['pdescription'];
$plow = $_REQUEST['plow'] ;
$phigh = $_REQUEST['phigh'] ;
$purl = $_REQUEST['purl'] ;
$ponline = $_REQUEST['ponline'] ;
$image = "image <img src='http://www.website.com.au/$n' />";

if (!isset($_REQUEST['email'])) {
    echo "we need your name and email address please" ;
  }

elseif (empty($name)){
echo "please enter your name";
}
elseif (empty($email)){
echo "please enter your email address";
}
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})", "$email")) {
echo"please check your email address - it doesn't appear to be a valid format";
}
else {
    mail( "admin@website.com.au", "subject",
     "$name\n $email\n $phone\n $address\n $pname\n $pdescription\n $plow\n $phigh\n $purl\n $ponline\n $image\n ", "$email");
   
    header( "Location: ../index.php" );
  }


?>

 

and the upload-image code

<?php if($_FILES)
{ 
$img = $_FILES['filename']['img'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg' : $ext = 'jpg'; break;
case 'image/png' : $ext = 'png'; break;
default: $ext = ''; break;
}
$target_path = "uploaded-images/";
$target_path = $target_path .time(). "-" . basename( $_FILES['filename']['name']);
if ($ext)
{
$n = "$target_path";
move_uploaded_file($_FILES['filename']['tmp_name'], $target_path);
echo "<span class='uploaded-photo'>The file ".  basename( $_FILES['filename']['name']). 
    " has been uploaded</span>";
echo "<h3>Great Photo!</h3><br /><img src='$n' /><br /><p>That was part 1, now for the rest of your details :</p>";
}
else echo "<h3>oops!</h3><br /><p>That is not a supported image file</p>";
}
else echo "<span class='uploaded-photo'>No image has been uploaded</span>";

?>

 

Thanks

 

@webecho

 

Link to comment
Share on other sites

Hi Joel

Yes they are separate.

 

I've added the both to the same file now, but still the same result.

 

I've tried the image upload script at top and mail script underneath and the other way around.

 

Same result in the email, I just get the domain url?

Link to comment
Share on other sites

try this

 

//mail code
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$pname = $_REQUEST['pname'] ;
$pdescription = $_REQUEST['pdescription'];
$plow = $_REQUEST['plow'] ;
$phigh = $_REQUEST['phigh'] ;
$purl = $_REQUEST['purl'] ;
$ponline = $_REQUEST['ponline'] ;
//default to none uploaded, then overwrite if image uploaded
$emailImage = "None Uploaded";

//get picture
if($_FILES)
{ 
$img = $_FILES['filename']['img'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg' : 
$ext = 'jpg'; 
break;

case 'image/png' : 
$ext = 'png'; 
break;

default: $ext = ''; 
break;
}
if ($ext)
{
$n = "image.$ext";
move_uploaded_file($_FILES['filename']['tmp_name'], $n);
echo "<span class='uploaded-photo'>uploaded image '$img'  as '$n':</span> <br />";
echo "<img src='$n' />";
$emailImage = "<img src='http://{$_SERVER["SERVER_NAME"]}$n' />";
}
else echo "'$img' is not a supported image file";
$emailImage = 'Image not of supported types';
}

if (!isset($_REQUEST['email'])) {
    echo "we need your name and email address please" ;
  }

elseif (empty($name)){
echo "please enter your name";
}
elseif (empty($email)){
echo "please enter your email address";
}
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})", "$email")) {
echo"please check your email address - it doesn't appear to be a valid format";
}
else {
    mail( "admin@website.com.au", "subject",
     "$name\n $email\n $phone\n $address\n $pname\n $pdescription\n $plow\n $phigh\n $purl\n $ponline\n $emailImage\n ", "$email");
   
    header( "Location: ../index.php" );
  }


Link to comment
Share on other sites

Hi Joel

 

Now it's coming through with None Uploaded where I would expect the photo to be.

 

I think my issue must be in the way the form is handling it.

 

I'm not going to get a proper chance to play with it until tomorrow now, but your explanations and new code have definitely helped me make sense of what's going on.

 

I'll let you know how I get on tomorrow

 

And thanks mate

 

 

@webecho

Link to comment
Share on other sites

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.