Jump to content

Problems with PHP form and image mailer


BenMo

Recommended Posts

I recently outsourced to have a programmer write a php form for me to use that would collect text and images from a website and email them to my inbox.  The files worked on his server, but when I uploaded them to my server I receive errors and the images do not successfully attach to the email.  Do I need to change some settings on my servers or something?  I really have no idea and any help is appreciated.  The errors I received looked like this...

 

Warning: move_uploaded_file(imgs/20070203205052_attachment1.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/phpAcrPgk' to 'imgs/20070203205052_attachment1.jpg' in /home/benjammin9/www/brb/mailer.php on line 67

Warning: fopen(imgs/20070203205052_attachment1.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 73

Warning: filesize(): Stat failed for imgs/20070203205052_attachment1.jpg (errno=2 - No such file or directory) in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fread(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fclose(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 75

Warning: move_uploaded_file(imgs/20070203205052_attachment2.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/php7KDvnP' to 'imgs/20070203205052_attachment2.jpg' in /home/benjammin9/www/brb/mailer.php on line 67

Warning: fopen(imgs/20070203205052_attachment2.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 73

Warning: filesize(): Stat failed for imgs/20070203205052_attachment2.jpg (errno=2 - No such file or directory) in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fread(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fclose(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 75

Warning: move_uploaded_file(imgs/20070203205052_attachment3.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/phpwdtnuk' to 'imgs/20070203205052_attachment3.jpg' in /home/benjammin9/www/brb/mailer.php on line 67

Warning: fopen(imgs/20070203205052_attachment3.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 73

Warning: filesize(): Stat failed for imgs/20070203205052_attachment3.jpg (errno=2 - No such file or directory) in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fread(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fclose(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 75

Warning: move_uploaded_file(imgs/20070203205052_attachment4.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/php3plpBP' to 'imgs/20070203205052_attachment4.jpg' in /home/benjammin9/www/brb/mailer.php on line 67

Warning: fopen(imgs/20070203205052_attachment4.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 73

Warning: filesize(): Stat failed for imgs/20070203205052_attachment4.jpg (errno=2 - No such file or directory) in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fread(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fclose(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 75

Warning: move_uploaded_file(imgs/20070203205052_attachment5.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/phpivdDIk' to 'imgs/20070203205052_attachment5.jpg' in /home/benjammin9/www/brb/mailer.php on line 67

Warning: fopen(imgs/20070203205052_attachment5.jpg): failed to open stream: No such file or directory in /home/benjammin9/www/brb/mailer.php on line 73

Warning: filesize(): Stat failed for imgs/20070203205052_attachment5.jpg (errno=2 - No such file or directory) in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fread(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 74

Warning: fclose(): supplied argument is not a valid stream resource in /home/benjammin9/www/brb/mailer.php on line 75

 

If you need to look at the code, then the HTML form code runs thusly...

 

<!-- Begin form -->
</p>
  <form action="mailer.php" method="post" enctype="multipart/form-data">
    <table summary="feedback form" border="0" cellpadding="8" cellspacing="8">
<tbody><tr>
          <td>Child's First Name:
            <input type="hidden" name="page" value="info.html" />
            <input type="hidden" name="recipient" value="BigRed" /> 
		<input type="hidden" name="subject" value="Child Information Form" /> 
            <input type="hidden" name="required" value="name, gender, mom, dad, hobby, neighborhood, message1, message2, message3, finalmessage, email, email2, referral, attachment1, attachment3, attachment5" />
            <input type="hidden" name="print_blank_fields" value="true" />
            <input name="alias" type="hidden" id="alias" value="name=Child First Name, mom=Mother's Nickname, dad=Father's Nickname, hobby=Child's Hobby, neighborhood=Neignborhood, message1=1st Message to Child from someone special, message2=2nd Message to Child from someone special, message3=3rd message to child from someone special, finalmessage=Final Message, email=Email Address, email2=Confirm Email Address, attachment1=Image of Child's Face, attachment2=2nd Image of Child's Face, attachment3=Image of Child's Family, attachment4=2nd image of Child's Family, attachment5=Image of Child's House" /> </td>
          <td><input name="name" type="text" size="25"></td></tr>
<tr><td>Gender (M or F):</td><td><input name="gender" type="text" size="2"></td></tr>
<tr><td>Nickname that child calls mother (Examples: "Mom", "Mommy", "Ma"):</td><td><input name="mom" type="text" size="15"></td></tr>
<tr><td>Nickname that child calls father (Examples: "Dad", "Daddy"):</td><td><input name="dad" type="text" size="15"></td></tr>
<tr><td>Child's Hobby (Examples: Swimming, Dance, Drawing):</td><td><input name="hobby" type="text" size="25"></td></tr>
<tr><td>Neighborhood, town, or location that the child most associates with home:</td><td><input name="neighborhood" type="text" size="25"></td></tr>

<tr><td>Message to the Child from someone special(such as friends, teachers, etc):</td><td>
<textarea rows="5" cols="35" name="message1">
</textarea></td></tr>

<tr><td>A second message to the Child from someone special:</td><td>
<textarea rows="5" cols="35" name="message2">
</textarea></td></tr>

<tr><td>A third message to the Child from someone special:</td><td>
<textarea rows="5" cols="35" name="message3">
</textarea></td></tr>

<tr><td>A final message from you to the Child(Will be displayed at the end of the game):</td><td>
<textarea rows="6" cols="40" name="finalmessage"></textarea></td></tr>
</tbody></table>

<h1>Contact Information and Referral</h1>
<p>
</p><table summary="feedback form" border="0" cellpadding="8" cellspacing="8">
<tbody><tr><td>Email address (Software download will be sent here):</td><td><input name="email" size="25" type="text"></td></tr>

<tr><td>Confirm email address:</td><td><input name="email2" size="25" type="text"></td></tr>

<tr><td>Did anyone refer you to our product? (First and last name):</td><td><input name="referral" size="25" type="text"></td></tr>
</tbody></table>

<h1>Source Images</h1>
<p>
</p><table summary="feedback form" border="0" cellpadding="8" cellspacing="5">
<tbody><tr><td>Image of Child's Face:</td>
          <td><input name="attachment1" type="file" size="45" id="attachment1" /> <br></td></tr>
<tr><td>Second Image of Child's Face (Optional, but recommended):</td><td><input name="attachment2" size="45" type="file"><br></td></tr>

<tr><td>Image of Child's Family:</td><td><input name="attachment3" size="45" type="file"><br></td></tr>
<tr><td>Second Image of Child's Family (Optional, but recommended):</td><td><input name="attachment4" size="45" type="file">
            <br></td></tr>

<tr><td>Image of Child's House:</td><td><input name="attachment5" size="45" type="file"><br></td></tr>
<tr>

</tr>

<tr>

<td colspan="2" align="center">

<input value="Send" type="submit"><br><small>

</small></td>

</tr>

</tbody></table>

</form> <!-- End Form -->

 

and the form ran like this...

 

<?

// setup the form content for use
$form = array();
$fieldname_lookup = array();
$in_array_func="";
$invis_array=array();

initialize();

// setup email (sender, subject, and body content
$email_from = 'Web Agent <[email protected]>'; 
$email_subject = $_POST['subject'];

// to avoid spamming - hide address on form
//$recipient_array = array('BigRed'=>'[email protected]');
//$email_to=$recipient_array[$_POST['recipient']];
$email_to="[email protected]"; // to add more emails, simply comma and space

// setup the email body content - from the form
reset($form);
$mailbody  = "<h3>The following information has been submitted:</h3>";
while (list($key,$val) = each($form)) {
	if ((!$in_array_func($key,$invis_array)) && ((isset($form['print_blank_fields'])) || ($val))) {
		if (isset($fieldname_lookup[$key])) { $key = $fieldname_lookup[$key]; } 
		$key=strtoupper($key);
		$mailbody .= "<div><b>".htmlspecialchars($key);
		$mailbody .= '</b>: ' . nl2br(htmlspecialchars(stripslashes($val))) . "</div>";
	}
}

// email plumbing work
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers = "From: ".$email_from.
		"\nMIME-Version: 1.0\n" . 
            "Content-Type: multipart/mixed;\n" . 
            " boundary=\"{$mime_boundary}\""; 

$email_message=$mailbody;
$email_message .= "This is a multi-part message in MIME format.\n\n" . 
				"--{$mime_boundary}\n" . 
				"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
			   "Content-Transfer-Encoding: 7bit\n\n" . 
$email_message . "\n\n"; 

// let's loop through the attachments
// special trick
// get count of attachments
for ($r=1; $r<=5; $r++){
	$att="attachment".$r;
	if(strlen($_FILES[$att]['name'])>0){
		if(strlen($att_cnt)!=0){ $att_cnt.=",";}
		$att_cnt.=$att;
	}
}
$att_active = explode(',', $att_cnt);
for ($r=0; $r<count($att_active); $r++){
	$att=$att_active[$r];
	if(strlen($_FILES[$att]['name'])>0){
		$ext=substr($_FILES[$att]['name'],strlen($_FILES[$att]['name'])-4,4);

		$filename=date("YmdHis")."_".$att.$ext;
		$uploadfile = "imgs/".$filename;

		move_uploaded_file($_FILES[$att]['tmp_name'], $uploadfile);

		$fileatt=$uploadfile;
		$fileatt_type = "application/octet-stream";
		$fileatt_name =$_FILES[$att]['name'];

		$file = fopen($fileatt,'rb'); 
		$data = fread($file,filesize($fileatt)); 
		fclose($file);

		$data = chunk_split(base64_encode($data)); 

		if($r==0){ $email_message .= "--{$mime_boundary}\n"; }
		$email_message .= "" . 
						  "Content-Type: {$fileatt_type};\n" . 
						  " name=\"{$fileatt_name}\"\n" . 
						  "Content-Transfer-Encoding: base64\n\n" . 
						 $data . "\n\n";
		if($r==count($att_active)-1){ $email_message.="--{$mime_boundary}--\n"; }
		else { $email_message.="--{$mime_boundary}\n"; }

		unset($data);
		unset($file);
		unset($fileatt);
		unset($fileatt_type);
		unset($fileatt_name);
	}
}

$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
?>
<div align="center">
  <table cellSpacing="8" width="98%" border="0" id="table1" bordercolorlight="#000000">
    <tr>

      <td class="small" colSpan="2"><div align="center">
          <p><font face="Verdana" size="2"> </font><b><font face="Verdana" size="4" color="#FF0000"><u> 
            </u></font></b></p>
          <p><b><font face="Verdana" size="4" color="#FF0000"><u>Thank you!</u></font></b> 
          </p>
        </div>
        <p align="center"> <b><font face="Verdana" size="2">Your information has 
          been <font color="#FF0000">submitted</font>. <br>
          You will hear back from us <u>via e-mail </u>within 24 business hours.<br>
          <br>
          <br>
          <br>
          </font></b></p>
	<p></b></td>
</tr>	
      <td align="center"> <font face="Arial" size="1"> Copyright ゥ 1999-2005, 
        BigRedButton</font> </table>
</div>
<?
} else { 
	die("Sorry but the email could not be sent. Please go back and try again!"); 

} 
?> 

<?
###############################################33
# special supporting function segment
function decode_vars()
{
if (isset($_REQUEST))
	$request = '_' . getenv('REQUEST_METHOD');
else
	$request = 'HTTP_' . getenv('REQUEST_METHOD') . '_VARS';
global $$request;
if (count($$request) > 0) {
	while (list($key, $val) = each($$request)) {
		if (is_array($val))
			$val = implode(', ',$val);
		$output[$key] = stripslashes($val);
	}
	return $output;
} else
	return array();
}

function check_required(){
global $form;
$error="";

if (isset($form['required'])) {
	$required = split(',', $form['required']);

	while (list(,$val) = each($required)) {
		$val = trim($val);
		if ((!isset($form[$val])) || (isset($form[$val]) && (strlen($form[$val]) < 1))) {
			// we got problem here
			$field = $val;
			if(substr($field,0,10)=="attachment"){
				if (!isset($_FILES[$field]['name']) || (isset($_FILES[$field]['name']) && (strlen($_FILES[$field]['name']) < 1)) ){
					$error.= "<li>".$field."</li>";
					$hit++;
				}
			} else {
				$error.= "<li>".$field."</li>";
				$hit++;
			}

		}
	}

	if ($hit>0){
		$error="<h1>Errors encountered</h1><H4>The following fields must be filled in:</h4><ul>".$error."</ul>";
		print $error;
		return 0;
	} else {
		return 1;
	}
}
return 1;
}

function alias_fields()
{
global $form, $fieldname_lookup;
while (list($key,) = each($form)) {
	$fieldname_lookup[$key] = $key;
}
reset($form);
if (isset($form['alias'])) {
	$aliases = explode(',', $form['alias']);
	while (list(,$val) = each($aliases)) {
		$temp = explode('=', $val);
		$fieldname_lookup[trim($temp[0])] = trim($temp[1]);
	}
}
return true;
}

function fake_in_array($needle, $haystack)
{
$found = false;
while (list($key,$val) = each ($haystack)) {
	if ($needle == $val)
		$found = true;
}
return $found;
}

function initialize(){

global $form, $in_array_func, $invis_array;

$form=decode_vars();
alias_fields();
if (phpversion() >= '4.0.0')
	$in_array_func = 'in_array';
else
	$in_array_func = 'fake_in_array';	


// lets validate first
if (check_required()==0){
	exit;
}

$invis_array = array('recipient','subject','required','redirect',
	     'print_blank_fields', 'sort','subject','title', 'page',
	     'link','css','hidden','alias');

}
?>

 

THANK YOU SO MUCH FOR ANY HELP AT ALL!

Link to comment
https://forums.phpfreaks.com/topic/36985-problems-with-php-form-and-image-mailer/
Share on other sites

I put both the mailer.php and the info.html form in the same directory.  Perhaps there's a hard-coded string that points to some directory...?

 

Also, I don't really expect anyone to look through that code; I just posted it just in case.  I'm 99.9% sure that the problem is very simple, like putting something in the right directory.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.