Jump to content
xisto Community
Sign in to follow this  
caronthegerman

Combining A Php Email And A File Uploader. I need to combine a form sent by email using php and a file uploader.

Recommended Posts

Hi guys,

I have pieced together two fine pieces of code but need to put these two puzzle pieces together correctly.

So, this is my upload script:

<form enctype="multipart/form-data" action="upload.php" method="POST">  <table align="left" cellpadding="5" cellspacing="5" border="0" width="100%" bgcolor=#FFFFFF>    <tr>      <td align="left" valign="top">        Select A File      </td>       <td align="left" valign="top">        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">        <input name="uploaded_file" type="file">      </td>    </tr>     <tr>      <td align="left" valign="top">               </td>       <td align="left" valign="top">        <input type="submit" value="Upload This File">      </td>    </tr>  </table></form>

With upload.php being:
     ($_FILES['uploaded_file']['type'] == 'image/png')    ||
($_FILES['uploaded_file']['type'] == 'image/jpeg') ||
($_FILES['uploaded_file']['type'] == 'image/jpg') ||
($_FILES['uploaded_file']['type'] == 'image/pjpeg')) &&
($_FILES['uploaded_file']['size'] < 2000000)) {



if ($_FILES['uploaded_file']['error'] > 0) {
echo "Error linenums:0'><?phpif ((($_FILES['uploaded_file']['type'] == 'image/gif') || ($_FILES['uploaded_file']['type'] == 'image/png') || ($_FILES['uploaded_file']['type'] == 'image/jpeg') || ($_FILES['uploaded_file']['type'] == 'image/jpg') || ($_FILES['uploaded_file']['type'] == 'image/pjpeg')) && ($_FILES['uploaded_file']['size'] < 2000000)) {if ($_FILES['uploaded_file']['error'] > 0) { echo "Error: " . $_FILES['uploaded_file']['error'] . "<br>";} else { echo "Upload: " . $_FILES['uploaded_file']['name'] . "<br>"; echo "Type: " . $_FILES['uploaded_file']['type'] . "<br>"; echo "Size: " . ($_FILES['uploaded_file']['size'] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES['uploaded_file']['tmp_name'] . "<br>"; $target_path = "useruploads/"; $target_thumb_path = "useruploadstrue/"; if(!is_writable($target_path)) { die('Cannot be uploaded to the specified directory. Please change permission to 777.'); } if(!is_writable($target_thumb_path)) { die('Thumbnail cannot be created. Please change permission to 777.'); } $uploaded_file = $_FILES['uploaded_file']['tmp_name']; $src = imagecreatefromjpeg($uploaded_file); list($width, $height)=getimagesize($uploaded_file); $new_width = 150; $new_height = ($height/$width)*$new_width; $tmp = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $filename = $target_thumb_path . $_FILES['uploaded_file']['name']; imagejpeg($tmp, $filename, 100); imagedestroy($src); imagedestroy($tmp); $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "The file " . basename( $_FILES['uploaded_file']['name']) . " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; }} }?>



The second piece of the puzzle is my email form. This is the html part:
<form action="mailform.php" method="post" name="form1" id="form1"><table width="450" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td background="styles/images/gif/clearpixel.gif" class="text"><p><span class="required">User Name:</span><br><input type="text" name="user_name" size="35" maxlength="50" /><br><br><span class="required">Real Name:</span><br /><input type="text" name="real_name" size="35" maxlength="50" /><br><br><span class="required">Image Source:</span><br /><input type="text" name="image_source" size="35" maxlength="75" /><br><br><span class="required">Email:</span><br /><input type="text" name="email" size="35" maxlength="100" /><br><br><span class="notrequired">Tell us which one of the followingyou would like:</span> <br /><input type="radio" name="category" onclick="updateTo('1');" />Please credit me for finding this picture.<br /><input type="radio" name="category" onclick="updateTo('2');" />Please do not credit me for finding this picture.<br /><input type="radio" name="category" onclick="updateTo('3');" />Please credit me for finding this picture only in the forums when the new pictures topic appears.<br /><br><input type="hidden" name="request_category" value=" " /><span class="notrequired">Please give any additional informationyou think will be useful.</span><br /><textarea name="request" rows="5" cols="38"></textarea><br><br><input type="submit" name="Submit" value="Submit" /></p></td></tr></table></form>

And this is my mailform.php:
$user_name= $_POST['user_name']; 
$real_name= $_POST['real_name'];
$image_source= $_POST['image_source'];
$email= $_POST['email'];
$category= $_POST['category'];
$request_category= $_POST['request_category'];
$request= $_POST['request'];
$message= $_POST['message'];

$body = "
User Name linenums:0'><?php $upload_name= $_POST['uploaded'];$user_name= $_POST['user_name']; $real_name= $_POST['real_name']; $image_source= $_POST['image_source']; $email= $_POST['email']; $category= $_POST['category']; $request_category= $_POST['request_category']; $request= $_POST['request']; $message= $_POST['message']; $body = " User Name: $user_name Real Name: $real_name Name of Image: $upload_nameImage Source: $image_source Email: $email Request Category: $request_category Additional Info: $request$message "; if (!isset($email) || !isset($user_name)) { header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } elseif (empty($email) || empty($user_name)) { header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } else { mail("admin_caron@loldmypants.com","From: loldmypants.com visitor",$body); header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } ?>

What I would like to do is to put the two php files into one and the two forms into one. I would also like to have the name or full url of the uploaded image to be called by $upload_name and to be displayed in the $body of the email that will be sent.

If you want to help, please do. Thanks :)

Share this post


Link to post
Share on other sites

Hmm, Caron it would appear you are taking code snippets from all over the web and are wanting us to piece them together for you as with the previous uploader and random image viewing scripts I helped you out with today.It's not that i'm not willing to help, i'm more than happy to help, however the best way to learn is through practical examples. Looking above it isn't going to be very hard to combine the two scripts, just have a look through them. It does seem like a rather simple process, I will give you a quick idea, place the contents of the upload.php in to your mailform.php and not the other way around (placing the contents of upload.php in the same else statement as the mail() function). Place your upload form in to your mail form, removing the image_source element from the original mail form first.If you haven't been able to accomplish this by the morning then I'll help you out with it, but practice will be the best thing for you i'm sure.

Edited by 8ennett (see edit history)

Share this post


Link to post
Share on other sites

Yea you're right. I was getting kind of lazy and dependent on you guys. Heh.

I put the two php files together and the html forms as well. It work as far as I know. I will post the two up.


uploadimage.html

<font color="black"><form enctype="multipart/form-data" action="uploadandmailform.php" method="post" name="form1" id="form1">  <table align="center" cellpadding="5" cellspacing="5" border="0" width="60%" bgcolor=#D2B48C>    <tr>      <td align="left" valign="top">        <p>Select A File<br>      </td> 	</tr> 	 	<tr>      <td align="left" valign="top">        <p><input type="hidden" name="MAX_FILE_SIZE" value="2000000">        <input name="uploaded_file" type="file">      </td>    </tr>     <tr>       <td align="left" valign="top">		       </td>    </tr>        <tr>      <td align="left" valign="top">		<span class="required">User Name:</span><br>		<input type="text" name="user_name" size="35" maxlength="50" />      </td>     </tr>          <tr>      <td align="left" valign="top">      	<span class="required">Real Name:</span><br>      	<input type="text" name="real_name" size="35" maxlength="50" />      </td>    </tr>        <tr>    	<td align="left" valign="top">    		<span class="required">Image Source:</span><br>    		<input type="text" name="image_source" size="35" maxlength="50" />    	</td>    </tr>        <tr>    	<td align="left" valign="top">    		<span class="required">Email:</span><br>    		<input type="text" name="email" size="35" maxlength="50" /><br><br>    	</td>    </tr>        <tr>    	<td align="left" valign="top">			<span class="notrequired">Tell us which one of the following you would like:</span> <br />				<input type="radio" name="category" onclick="updateTo('1');" />Please credit me for finding this picture.<br />				<input type="radio" name="category" onclick="updateTo('2');" />Please do not credit me for finding this picture.<br />				<input type="radio" name="category" onclick="updateTo('3');" />Please credit me for finding this picture only in the forums when the new pictures topic appears.<br /><br>				<input type="hidden" name="request_category" value=" " />		</td>	</tr>	<tr>		<td align="left" valign="top">			<input type="hidden" name="request_category" value=" " />			<span class="notrequired">Please give any additional information you think will be useful.</span><br />			<textarea name="request" rows="5" cols="38"></textarea><br><br>		</td>	</tr>		<tr>		<td align="left" valign="top">			 		</td>		<td align="left" valign="top">			 		</td>	</tr>		<tr>		<td align="left" valign="top">			<input type="submit" name="Submit" value="Submit" />		</td>	</tr>	</p>  </table></form></font>


uploadandmailform.php
     ($_FILES['uploaded_file']['type'] == 'image/png')    ||
($_FILES['uploaded_file']['type'] == 'image/jpeg') ||
($_FILES['uploaded_file']['type'] == 'image/jpg') ||
($_FILES['uploaded_file']['type'] == 'image/pjpeg')) &&
($_FILES['uploaded_file']['size'] < 2000000)) {



if ($_FILES['uploaded_file']['error'] > 0) {
echo "Error linenums:0'><?php if ((($_FILES['uploaded_file']['type'] == 'image/gif') || ($_FILES['uploaded_file']['type'] == 'image/png') || ($_FILES['uploaded_file']['type'] == 'image/jpeg') || ($_FILES['uploaded_file']['type'] == 'image/jpg') || ($_FILES['uploaded_file']['type'] == 'image/pjpeg')) && ($_FILES['uploaded_file']['size'] < 2000000)) {if ($_FILES['uploaded_file']['error'] > 0) { echo "Error: " . $_FILES['uploaded_file']['error'] . "<br>";} else { echo "Upload: " . $_FILES['uploaded_file']['name'] . "<br>"; echo "Type: " . $_FILES['uploaded_file']['type'] . "<br>"; echo "Size: " . ($_FILES['uploaded_file']['size'] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES['uploaded_file']['tmp_name'] . "<br><br>"; $target_path = "useruploads/"; $target_thumb_path = "useruploadstrue/"; if(!is_writable($target_path)) { die('Cannot be uploaded to the specified directory. Please change permission to 777.'); } if(!is_writable($target_thumb_path)) { die('Thumbnail cannot be created. Please change permission to 777.'); } $uploaded_file = $_FILES['uploaded_file']['tmp_name']; $src = imagecreatefromjpeg($uploaded_file); list($width, $height)=getimagesize($uploaded_file); $new_width = 150; $new_height = ($height/$width)*$new_width; $tmp = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $filename = $target_thumb_path . $_FILES['uploaded_file']['name']; imagejpeg($tmp, $filename, 100); imagedestroy($src); imagedestroy($tmp); $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "The file " . basename( $_FILES['uploaded_file']['name']) . " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; }} }$upload_name= $_POST['uploaded'];$user_name= $_POST['user_name']; $real_name= $_POST['real_name']; $image_source= $_POST['image_source']; $email= $_POST['email']; $category= $_POST['category']; $request_category= $_POST['request_category']; $request= $_POST['request']; $message= $_POST['message']; $body = " User Name: $user_name Real Name: $real_name Name of Image: " . basename( $_FILES['uploaded_file']['name']) . "Image Source: $image_source Email: $email Request Category: $request_category Additional Info: $request$message "; if (!isset($email) || !isset($user_name)) { header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } elseif (empty($email) || empty($user_name)) { header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } else { mail("admin_caron@loldmypants.com","From: loldmypants.com visitor",$body); header("Location: [url="http://forums.xisto.com/no_longer_exists/; "); } ?>
Sorry that I made it long again. :)
Edited by caronthegerman (see edit history)

Share this post


Link to post
Share on other sites

It's looking good, as long as it works lolSo next what are you going to do with the rest of the data the user has submitted? It uploads the image file ok, but the user fills out the rest of the form yet you don't store any of that data. You give them the option to credit them, don't credit them, and for some reason credit them on the forum which you don't seem to have yet.Are you going to be putting this information in to a database? Or even just storing it in a text file?

Share this post


Link to post
Share on other sites

It's looking good, as long as it works lol
So next what are you going to do with the rest of the data the user has submitted? It uploads the image file ok, but the user fills out the rest of the form yet you don't store any of that data. You give them the option to credit them, don't credit them, and for some reason credit them on the forum which you don't seem to have yet.

Are you going to be putting this information in to a database? Or even just storing it in a text file?



Well since I didn't know how to do any of that stuff I just kind of wanted the information to be sent to my email so that in case anybody uploads something I know who uploaded it and if I can view the image and see if it is suitable and allowed.

I didn't really think much about the credit thing, but I would probably post their name in the forum topic I make every day or so shows new pictures on a newpics#.html, which I post on the forum for anybody who wants to see the new pictures and not every picture.

Here is an example: (the pictures don't work anymore because I put them in picturearchive/archive/, but since I don't have any users who care about new pictures yet, it doesn't matter that much. I'm putting up a new link and a new newpics5.html soon)

4 new pictures added today.
Check them out:
http://forums.xisto.com/no_longer_exists/


These will be left up for approximately 4 days.

Taken from this forum topic.
Edited by caronthegerman (see edit history)

Share this post


Link to post
Share on other sites

You probably should learn how to use databases with php as it is probably one of the most powerful and flexible ways to create any kind of website that stores user data.

Try going through the tutorial here

When I first decided to learn php it was this tutorial I used, I managed to go through the entire thing within 24 hours and by the time I had finished it I knew everything I needed to know about using PHP and MySQL together. It taught me all the basics of securing my site and its contents, how to use custom functions and classes, if statements and loops, arrays and sql data. Everything you need to make life so much easier for yourself.

It would be easiest for you to do it this way and learn how to do it, that way you won't have to approve every single image uploaded using information sent to your email (it will get very old very fast, trust me). Instead the whole system will be automated (including adding credit to images on your site etc.) and all you will have to do is click a single button everytime an image is uploaded to approve it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.