Jump to content

Struggling with file uplaoding issue


Merdok

Recommended Posts

Hi Guys, I'm trying to build a form which includes a file field. The idea is that it will upload the file and then email it as an attachment, I couldnt' figure that out at all so I thought I'd go with uploading the file and including a link to it in the email, however that isn't working either and I don't understand why... i'm not getting any error messages and the upload folder is set to 777.

 

	if (($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      $fileLink = $siteroot. "/upload/" . $_FILES["file"]["name"];
      }
  }
else
  {
  echo "Invalid file";
  }
$to = str_replace('info', 'sales', $sc_email);  
$subject = "Quotation request from website";
$body = "From: ".$_POST['first_name']." ".$_POST['last_name']."\n";
if ($_POST['job_title']) {
$body .= "(".$_POST['job_title']." at ".$_POST['your_company'].")\n";
}
$body .= "E-Mail: ".$_POST['your_email']."\n";
$body .= "Telephone: ".$_POST['your_telephone']."\n";
$body .= "Address:\n ".$_POST['your_address']." ,".$_POST['your_country']."\n\n";
$body .= "Project: ".$_POST['project_details']."\n";
$body .= "Translate to: ".$_POST['lang_target']." from ".$_POST['lang_source']."\n";
$body .= "by: ".$_POST['project_deadline'];
$body .= "The text: ".$_POST['text_to_translate'];
$body .= "Link to attachment: <a href=".$fileLink."> Link </a>";

 

Just helping me to figure out this would be enough but if anyone has a solution to the emailng as attatchment thing that would be even better. I've looked at a few tutorials but they seem a bit confusing.

 

Thanks in advance.

Link to comment
Share on other sites

Did you try to base64-encode the file before adding it as an attachment?

 

http://be.php.net/manual/en/function.base64-encode.php

 

Yes I believe base64 encoding was a part of the tutorial

 

Here is the code for the full page:

 


<?php 
// BEGIN SOCKET IMPLEMENTATION 
require_once($_SERVER['DOCUMENT_ROOT'] . '/socket/globals.php'); //Sets the static pageID 

if ($_GET['article']) {
if ($_GET['revision']) { // If viewing a revision from SOCKET
$dblookup = "SELECT articleID, datePosted, articleTitle, permaLink, articleBody, sideBody, parent FROM core_pages_revisions WHERE(revisionID ='".$_GET['revision']."') LIMIT 1";		
} else {
$dblookup = "SELECT articleID, datePosted, articleTitle, permaLink, articleBody, sideBody, parent FROM core_pages WHERE(articleID ='".$_GET['article']."') OR (permaLink ='".$_GET['article']."'";
if($_GET['parent']) {																																											$dblookup .= " AND parent ='".$_GET['parent']."'";
}
$dblookup .= ") LIMIT 1";
}
$data = mysql_query($dblookup) or die('Failed to return data: ' . mysql_error());	
if (mysql_num_rows($data) == 0) { redirect_to('/404');exit;}
/* sorts the data into variables and puts them in an array ready to be called when needed */
while(list($articleID, $datePosted, $articleTitle, $permaLink, $articleBody, $sideBody, $parent) = mysql_fetch_array($data, MYSQL_BOTH))
{
$meta_title = html_entity_decode(stripslashes("$articleTitle"));
$module_ID = 2;
$theH1 = '<h1>' . stripslashes(html_entity_decode($articleTitle)) . '</h1>';
$theParent = $parent;
$parentUrl = $_SERVER['HTTP_REFERER'];
require_once('' . $serverroot . '/style/standard/head.php');
require_once('' . $serverroot . '/style/standard/head2.php');
require_once('style/standard/header.php');

if(isset($_POST['submit'])) {

/* Deals with file uploading */
if (($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      $fileLink = $siteroot. "/upload/" . $_FILES["file"]["name"];
      }
  }
else
  {
  echo "Invalid file";
  }
$to = str_replace('info', 'sales', $sc_email);  
$subject = "Quotation request from website";
$body = "From: ".$_POST['first_name']." ".$_POST['last_name']."\n";
if ($_POST['job_title']) {
$body .= "(".$_POST['job_title']." at ".$_POST['your_company'].")\n";
}
$body .= "E-Mail: ".$_POST['your_email']."\n";
$body .= "Telephone: ".$_POST['your_telephone']."\n";
$body .= "Address:\n ".$_POST['your_address']." ,".$_POST['your_country']."\n\n";
$body .= "Project: ".$_POST['project_details']."\n";
$body .= "Translate to: ".$_POST['lang_target']." from ".$_POST['lang_source']."\n";
$body .= "by: ".$_POST['project_deadline'];
$body .= "The text: ".$_POST['text_to_translate'];
$body .= "Link to attachment: <a href=".$fileLink."> Link </a>";

if ($errors) { echo $errors; } else {
	echo '<h1> Email Sent! </h1>';
	echo '<p>Thank you for your enquiry.</p> <p>If it is required a representative will contact you as soon as possible</p><p><strong>Please note:</strong></p>  <p>We endeavour to respond to all requests within ' . $sc_response_time . ' however during busy or holiday periods this may increase. </p>';
	mail($to, $subject, $body);
}

} else {

//Main content starts here
/****************************/
/*    Page starts here 		*/
/****************************/
//echo '<img class="hidden" src="'.$siteroot.'/Scripts/phpThumb/phpThumb.php?src='.$siteroot.'/Scripts/phpThumb/phpThumb.php?w=100&h=100&zc='.$articleImagePos.'&src='.$articleImage.'" />';
if ($_SESSION['access_lvl'] <= 1) {
// if a user with appropriate access levels is logged in, allow them to edit this page
// to add
}
echo stripslashes($articleBody);
?>
<form id="quote_form" name="quote_form" method="post" action="<?php $_SERVER['SCRIPT_NAME']?>">
<fieldset>
	<legend> Your Details </legend>
      	<table border="0" cellpadding="0" cellspacing="5">
            <tr>
            <td class="colOne"> Name </td>
              <td class="colTwo"><label for="first_name">First Name</label>
              <input type="text" name="first_name" id="first_name" value="First Name" onfocus="this.value=''" onblur="if (this.value = "") {this.value='First Name'}" /></td>
              <td><label for="last_name">Last Name</label>
              <input type="text" name="last_name" id="last_name" value="Last Name" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Last Name'}" /></td>
            </tr>
            <tr>
            	<td class="colOne"></td>
              <td class="colTwo"><label for="job_title">Job Title</label>
              <input type="text" name="job_title" id="job_title" value="Job Title" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Job Title'}"/></td>
              <td><label for="your_company">Company</label>
              <input type="text" name="your_company" id="your_company" value="Company" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Company'}"/></td>
            </tr>
            <tr>
		  <td class="colOne"> Contact </td>
              <td class="colTwo"><label for="your_telephone">Telephone</label>
              <input type="text" name="your_telephone" id="your_telephone"  value="Telephone" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Telephone'}" /></td>
              <td><label for="your_email">Email Address</label>
              <input type="text" name="your_email" id="your_email"  value="Email" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Email'}"/></td>
            </tr>
            <tr>
		  <td class="colOne"> Address </td>
              <td class="colTwo" valign="top"><label for="your_address">Address</label>
              <textarea name="your_address" id="your_address"  cols="45" rows="5" ></textarea></td>
              <td></td>
            </tr>
             <tr>
			<td class="colOne"> Country </td>
              	<td class="colTwo"><label for="your_country">Country</label>
                <select name="your_country" id="your_country">
                <option value="" selected="selected">Select your country</option>
                <?php $countries = mysql_query("SELECT * FROM custom_countries") or die ("Could not get countries list".mysql_error());			
	          while($option = mysql_fetch_array($countries)) {
        		  echo '<option value="' . $option['country_name'].'">' .$option['country_name'].'</option>';
        		  }
	         ?>
              </select></td>
              <td></td>
            </tr>
   		</table>
    </fieldset>
    <fieldset>
        <legend> Your Quote </legend>
        <table border="0" cellpadding="0" cellspacing="5">
		<tr>
			<td class="colOne"> Languages </td>
              	<td class="colTwo"><label for="lang_source">Source Language</label>
                <select name="lang_source" id="lang_source">
                <option value="" selected="selected">Translate from</option>
                <?php $languages = mysql_query("SELECT * FROM custom_languages") or die ("Could not get languages list".mysql_error());			
	          while($option = mysql_fetch_array($languages)) {
				  echo '<option value="' . $option['language_name'].'">' .$option['language_name'].'</option>';
        		  }
	         ?>
              </select>
              <label for="lang_target[]">Target Language</label></td>
              <td>(hold CTRL to select multiple languages)<select name="lang_target[]" size="6" multiple="multiple" id="lang_target[]">
                <option value="" selected="selected">Translate to</option>
                <?php $languages = mysql_query("SELECT * FROM custom_languages") or die ("Could not get languages list".mysql_error());			
	          while($option = mysql_fetch_array($languages)) {
				  echo '<option value="' . $option['language_name'].'">' .$option['language_name'].'</option>';
        		  }
	         ?>
              </select></td>
            </tr>
            <tr>
		  <td class="colOne"> Project</td>
              <td class="colTwo"><label for="project_details">Project Details</label>
              <input type="text" name="project_details" id="project_details"  value="Reference" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Reference'}" /></td>
              <td><label for="project_deadline">Project Deadline</label>
              <input type="text" name="project_deadline" id="project_deadline"  value="Deadline" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Deadline'}" /></td>
            </tr>
            <tr>
		  <td class="colOne"> Text to translate</td>
              <td colspan="2"><label for="ttt">Text to translate</label>
              <input type="file" name="ttt" id="ttt" value="" />
            </tr>

            <tr>
                <td> </td>
                <td><input class="form_button" type="submit" name="submit" id="submit" value="Submit" /><p class="red size10font"><?php echo $error ?></p></td>
            </tr>
         </table>
    </fieldset>
</form>

</div>
<?php  
}
//Main content ends here
require_once('style/standard/footer.php');
}
}
?>

Link to comment
Share on other sites

this is a good script that will let you send emails with attachments. it is a tutorial with a link so you can download the ready made form at the bottom. you need to install the PEAR Library, link to that is at the very bottom as well. the script is easy to edit and has validation and injection protection.

 

http://www.html-form-guide.com/email-form/php-email-form-attachment.html

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.