Yasitha
Yasitha

Reputation: 911

how to remove directory name from the attachment name in php

i have a small issue. i had upload a screen shot of a email that has an attachment sent by using php.but in the attachment name there is the directory name as "upload./" i want to remove that part. how can i do that?

Thank you.enter image description here

$message = $mes;
                echo $message;

                //echo $message;
                $subject = $coursetitle;
                $headers .= 'Cc: [email protected]' . "\r\n";

                ini_set("SMTP", "mail.amdt.lk");
                ini_set("sendmail_from", "[email protected]");

                // 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/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
                $message .= "--{$mime_boundary}\n";

                // preparing attachments




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



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

Upvotes: 0

Views: 311

Answers (3)

FrickX
FrickX

Reputation: 63

$string = str_replace("upload./", "", $string);

Thats all :)

Upvotes: 0

Marc B
Marc B

Reputation: 360702

You're doing it yourself...

"Content-Disposition: attachment;\n" . " filename=\"$dir.$files[$x]\"\n" 
                                                    ^^^^

that's the filename as it should appear in the client browser... it has nothing to do with where the file is on your server, or what its name is on your server. That's purely for the remote user's information.

In the greater scheme of things... don't build your own mime messages, or use mail(). Mime is painful to get right, when there's perfectly good libraries like PHPMailer and Swiftmailer that do all of that for you with far less code.

Upvotes: 2

zb&#39;
zb&#39;

Reputation: 8059

Use basename() function:

$file="upload./file.jpg";
$basename=basename($file);
$dirname=dirname($file);

Upvotes: 2

Related Questions