如何从php中的附件名称中删除目录名称

时间:2012-10-14 04:25:36

标签: php

我有一个小问题。我上传了一个电子邮件的屏幕截图,其中包含使用php发送的附件。但是在附件名称中,目录名称为" upload ./"我想删除那部分。我怎样才能做到这一点?

谢谢。enter image description here

$message = $mes;
                echo $message;

                //echo $message;
                $subject = $coursetitle;
                $headers .= 'Cc: gayani@amdt.lk' . "\r\n";

                ini_set("SMTP", "mail.amdt.lk");
                ini_set("sendmail_from", "info@amdt.lk");

                // 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>";
              }

3 个答案:

答案 0 :(得分:2)

使用basename()功能:

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

答案 1 :(得分:2)

你自己做的......

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

这是应该出现在客户端浏览器中的文件名...它与服务器上文件的位置或服务器上的名称无关。这纯粹是为了远程用户的信息。

在更大的方案中......不要构建自己的mime消息,也不要使用mail()。 Mime很难做到正确,当有像PHPMailer和Swiftmailer这样的非常好的库时,可以用更少的代码为你完成所有这些。

答案 2 :(得分:0)

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

多数民众赞成:)