Jump to content

Contact Form with File Attached?


ask9

Recommended Posts

Hi guys

 

I have created these codes below,

 

uploadform.php

<html>
<head>
</head>

<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table>
        <tr>
		<td><div align="left">Submit photo </div></td>
		<td><div align="left">
			<input type="file" name="file" id="file" />						
		</div></td>
        </tr>
        <tr>
		<td><div align="left"></div></td>
		<td><div align="left">
			<input type="submit" name="Submit" value="Submit" />
		</div></td>
        </tr>
</table>
</form>
</body>
</html>

 

 

upload.php

<?php
/************************
* Upload file
*************************/
if (isset($_POST['Submit'])) { //if "email" is filled out, send email
/*** Upload File ***/
if($_FILES["file"]["size"] < 20000)
{
	if ($_FILES["file"]["error"] > 0)
	{
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	}
	else
	{
	//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
	//echo "Type: " . $_FILES["file"]["type"] . "<br />";
	//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
	//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

	if (file_exists("upload/" . $_FILES["file"]["name"]))
	  {
	  //$_FILES["file"]["name"]
	  // Do nothing...
	  }
	else
	  {
	  move_uploaded_file($_FILES["file"]["tmp_name"],
	  "upload/" . $_FILES["file"]["name"]);
	  $url = "http://asiamodeltalent.com/php/mt/" . "upload/" . $_FILES["file"]["name"];
	  }
	}
}
else
{
	echo "Invalid file";
}

/************************
* Insert path and filename to array
*************************/	

$fname = $_FILES["file"]["name"];
$files = array("$fname");


/************************
* Send Message to email
*************************/	

$to = "iridion_us@yahoo.com";
$from = "contact@coder9.com"; 
$subject ="Email File"; 
$message = "Test email with file attached.\n";
$headers = "From: $from";

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

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

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

// preparing attachments
$pathupload = "http://coder9.com/php/mt/" . "upload/";	
for($x=0;$x<count($files);$x++){
	//$file = fopen($files[$x],"rb");
		$file = fopen($url[$x], "rb");				
	//$data = fread($file,filesize($files[$x]));
		$data = fread($file, filesize($url[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}

// send

$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 


}
?>

 

 

The problem with this codes is the file is attached but it's empty.

By the way if you want to test it you need a sub directory of /upload

What is the cause of this problem?

 

Thanks in advanced.

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.