Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting rid of temporary filename in attachement 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hello everyone,

I'm using the code below in order to add an attachement to an email built from a form.

The problem is that the filename of the attachement found in the email remains the one of the temporary file made during the upload.

How could I make sure that it's the real filename of the file that will appear in the email?

Thanks a lot to the ones who will help.

Cheers

Code:
				for($i=0; $i <= sizeof($file_fieldname); $i++ ) {

						if ($_FILES[$file_fieldname[$i]]['name'] != "") {

						$file_name = $_FILES[$file_fieldname[$i]]['name'];
						$file_type = $_FILES[$file_fieldname[$i]]['type'];
						$file_tmp_name = $_FILES[$file_fieldname[$i]]['tmp_name'];
						$file_cnt = "";

						$f=@fopen($file_tmp_name, "rb");
						
						if (!$f) continue;
						
						while($f && !feof($f))
						$file_cnt .= fread($f, 4096);
						fclose($f);
						
								if (!strlen($file_type)) $file_type="application/octet-stream";
								
								if ($file_type == 'application/x-msdownload') $file_type = "application/octet-stream";

						$message .= "--" . $separator . $br;
						$message .= "Content-Type: " . $file_type . ";" . $br;
						$message .= "name=\"" .$file_name  . "\"" . $br;
						$message .= "Content-Transfer-Encoding: base64" . $br;
						$message .= "Content-Disposition: attachment;" . $br;
						$message .= "filename=\"" . $file_name . "\"" . $br . $br;
						$message .= chunk_split(base64_encode($file_cnt)); 

						}
				
				}
 
That's odd. At no point have you supplied the temp name.

Have you looked at PHPMailer? That class has an AddAttachment() method that will simply add a file on the filesystem. You can provide an alternate name to use, too.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 

Thanks sleipnir214.

I had a look at the documentation and small tutorial provided on the PHPmailer site yesterday and I found it not very convenient for my current project. For example, I don't like the fact that you have to specify the smtp server and other things. I prefer to keep things simple when it's possible anyway.

You're right, in the code, I never specify the temp filename in the header.

Maybe there is some way to rename the temp file with the real filename just after the file content is gathered?

 
You don't necessarily have to specify the SMTP server. The class has three ways of getting the mail out: via PHP's mail() function, via a locally-installed sendmail app and via network sockets directly to an SMTP server. It's only necessary to set the host if you are using the third method.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top