无法访问文件:.在电子邮件附件中发送pdf


Could not access file:....Sending pdf in a email attachment

我使用php mailer类发送电子邮件,它工作正常。

但现在我试图在我的电子邮件中发送附件,我有一个错误。

当我点击我的按钮发送电子邮件时,我得到我的成功消息,电子邮件已成功发送,但我也得到这个消息"无法访问文件:pdfName.pdf"。

在我的文件发送电子邮件,我有这个php代码:

<?php
if(isset($_POST['sendForm'])){
    $subject = $_POST['subject'];
    $pdf = $_FILES['pdf']['name'];

    if(empty($subject) || empty($_FILES['pdf']['name'])){
        echo 'Please fill all fields';
    }
    else{
        $readSubscribers = $pdo->prepare("SELECT * FROM subscribers");  
        $readSubscribers->execute();
        if($readSubscribers->rowCount() <=0){
            echo 'We dont have any subscriber yet.';    
        }
        else{
            while($result = $readSubscribers->fetch(PDO::FETCH_ASSOC)){
            $email  = $result['email'];
            $code = md5($code);
            $msg = 'email message';  
            sendMail($subject,$msg,MAILUSER,SITENAME,$email,'',$pdf);
            }
            echo 'Email sent with sucess.';
        }
    }
}
?>
这是my html:
<form name="editpost" method="post" enctype="multipart/form-data"> 
    <div>
        <span>Subject:</span>
        <input type="text" name="subject" value="" />
    </div>
    <div>
        <span>Attachment:</span>
        <input type="file" name="pdf" accept="application/pdf" />                         
    </div>  
    <input type="submit" value="Send" name="sendForm"/>
</form>

如果我存储我的pdf石灰临时文件,像这样:

$pdf = $_FILES['pdf']['tmp_name'];

当我点击我的按钮发送电子邮件时,我没有任何错误信息,但是当我收到电子邮件时,电子邮件有这个名字:"phpF.tmp",并且无法打开…

在send php文件中试试:

//first rename the uploaded file:
$filename = "/tmp/".$_FILES['pdf']['name'];
rename($_FILES['pdf']['tmp_name'], $filename);
//you can also move the uploaded file with the command:
move_uploaded_file($_FILES['pdf']['tmp_name'], $filename);
...
//and then send the changed file
sendMail($subject, $msg, MAILUSER, SITENAME, $email, '', $filename);
//and if everything is fine delete the file:
unlink($filename);

添加:

你可以检查上传的文件是否来自pdf类型:

if(is_uploaded_file($_FILES['pdf']['tmp_name']) && strtolower(substr($_FILES['pdf']['name'], -4)) == ".pdf")
{
    //and do something if the file is uploaded:
    if(move_uploaded_file($_FILES['pdf']['tmp_name'], $filename)
    {
    }
}
//or check the mimetype
ob_start();
$type = system("file --mime-type -b ".$filename);
ob_clean();