Contact Form with File Attached?

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.\
";
	$headers = "From: $from";
	 
	// boundary 
	$semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	 
	// headers for attachment 
	$headers .= "\
MIME-Version: 1.0\
" . "Content-Type: multipart/mixed;\
" . " boundary=\\"{$mime_boundary}\\""; 
	 
	// multipart boundary 
	$message = "This is a multi-part message in MIME format.\
\
" . "--{$mime_boundary}\
" . "Content-Type: text/plain; charset=\\"iso-8859-1\\"\
" . "Content-Transfer-Encoding: 7bit\
\
" . $message . "\
\
"; 
	$message .= "--{$mime_boundary}\
";

	// 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\\"};\
" . " name=\\"$files[$x]\\"\
" . 
		"Content-Disposition: attachment;\
" . " filename=\\"$files[$x]\\"\
" . 
		"Content-Transfer-Encoding: base64\
\
" . $data . "\
\
";
		$message .= "--{$mime_boundary}\
";
	}
	 
	// 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.

Is the file empty on the server after you upload it, or is it uploading correctly but becoming zero length in the attachment? That is, does the upload part work but not the email part, or is the problem in the actual upload?