webecho Posted February 3, 2011 Share Posted February 3, 2011 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( "[email protected]", "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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/ Share on other sites More sharing options...
joel24 Posted February 3, 2011 Share Posted February 3, 2011 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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169324 Share on other sites More sharing options...
webecho Posted February 3, 2011 Author Share Posted February 3, 2011 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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169388 Share on other sites More sharing options...
joel24 Posted February 3, 2011 Share Posted February 3, 2011 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( "[email protected]", "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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169622 Share on other sites More sharing options...
webecho Posted February 4, 2011 Author Share Posted February 4, 2011 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( "[email protected]", "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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169692 Share on other sites More sharing options...
joel24 Posted February 4, 2011 Share Posted February 4, 2011 your variable, $n is not set. are the image upload and email scripts two separate scripts? i.e. uploadscript.php and email.php?? you need to put the two together so that the email script can see the variable $n... Link to comment https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169696 Share on other sites More sharing options...
webecho Posted February 4, 2011 Author Share Posted February 4, 2011 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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169701 Share on other sites More sharing options...
joel24 Posted February 4, 2011 Share Posted February 4, 2011 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( "[email protected]", "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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169740 Share on other sites More sharing options...
webecho Posted February 4, 2011 Author Share Posted February 4, 2011 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 https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1169744 Share on other sites More sharing options...
joel24 Posted February 4, 2011 Share Posted February 4, 2011 and also, don't use $_REQUEST > use $_POST if its a posted form or $_GET if they're variables sent via the URL. You want $_POST in your case Link to comment https://forums.phpfreaks.com/topic/226548-image-upload-and-email-simple-mistake-im-sure/#findComment-1170030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.