首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >上传的文件在php中不会保存在服务器中

上传的文件在php中不会保存在服务器中
EN

Stack Overflow用户
提问于 2021-01-07 12:48:51
回答 2查看 40关注 0票数 1

我正在尝试上传一个文件,并将该文件保存在服务器中,以及attac,并通过电子邮件发送该文件。如果文件不会保存在服务器的uploads文件夹中,则通过电子邮件连接和发送文件工作正常。如何将文件保存在服务器上,并通过电子邮件以附件的形式发送..以下是代码

代码语言:javascript
运行
复制
<form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm">
    <div class="form-group">
        <input type="text" name="name" id="name" class="form-control" placeholder="Name" >
       
    </div>
    <div class="form-group">
        <input type="email" name="email" id="email" class="form-control" placeholder="Email address" >
        
    </div>
    <div class="form-group">
        <input type="text" name="subject" id="subject" class="form-control"  placeholder="Subject" >
       
    </div>
    <div class="form-group">
        <textarea name="message" id="message" class="form-control" placeholder="Write your message here"></textarea>
      
    </div>
    <div class="form-group">
        <input type="file" name="attachment" id="attachment" class="form-control">
        <div id="attachmentError" style="color: red;font-size: 14px;display: none">attachmentError</div>
    </div>
    <div class="submit">
        <input type="submit" name="submit" onclick="return validateEmailSendForm();" class="btn btn-success" value="SUBMIT">
    </div>
</form>

email-script.php

代码语言:javascript
运行
复制
<?php

if(isset($_POST['submit'])){
    // Get the submitted form data
    $postData = $_POST;
    $email = $_POST['email'];
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $uploadStatus = 1;

        // Upload attachment file
    if(!empty($_FILES["attachment"]["name"])){

        // File path config
        $targetDir = "uploads/";
        $fileName = basename($_FILES["attachment"]["name"]);
        $targetFilePath = $targetDir . $fileName;
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

        // Allow certain file formats
        $allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
        if(in_array($fileType, $allowTypes)){
            // Upload file to the server
            if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){
                $uploadedFile = $targetFilePath;
            }else{
                $uploadStatus = 0;
                $statusMsg = "Sorry, there was an error uploading your file.";
            }
        }else{
            $uploadStatus = 0;
            $statusMsg = 'Sorry, only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload.';
        }
    }

    if($uploadStatus == 1){
        // Recipient
        $toEmail = $email;
        // Sender
        $from = 'sender@codingbirdsonline.com';
        $fromName = 'CodexWorld';
        // Subject
        $emailSubject = 'Email attachment request Submitted by '.$name;
        // Message
        $htmlContent = '<h2>Contact Request Submitted</h2>
            <p><b>Name:</b> '.$name.'</p>
            <p><b>Email:</b> '.$email.'</p>
            <p><b>Subject:</b> '.$subject.'</p>
            <p><b>Message:</b><br/>'.$message.'</p>';

        // Header for sender info
        $headers = "From: $fromName"." <".$from.">";

        if(!empty($uploadedFile) && file_exists($uploadedFile)){
            // 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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
            // Preparing attachment
            if(is_file($uploadedFile)){
                $message .= "--{$mime_boundary}\n";
                $fp =    @fopen($uploadedFile,"rb");
                $data =  @fread($fp,filesize($uploadedFile));
                @fclose($fp);
                $data = chunk_split(base64_encode($data));
                $message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" .
                    "Content-Description: ".basename($uploadedFile)."\n" .
                    "Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" .
                    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }

            $message .= "--{$mime_boundary}--";
            $returnpath = "-f" . $email;
            // Send email
            $mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
            // Delete attachment file from the server
            @unlink($uploadedFile);
        }else{
            // Set content-type header for sending HTML email
            $headers .= "\r\n". "MIME-Version: 1.0";
            $headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
            // Send email
            $mail = mail($toEmail, $emailSubject, $htmlContent, $headers);
        }

        // If mail sent
        if($mail){
            $statusMsg = 'Your contact request has been submitted successfully !';
        }else{
            $statusMsg = 'Your contact request submission failed, please try again.';
        }
    }
     echo '<script>alert("'.$statusMsg.'");window.location.href="./";</script>';
}

?>
EN

Stack Overflow用户

发布于 2021-01-07 15:48:41

unlink功能用于删除服务器上的文件,您可以很好地删除或注释掉该部分。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65606774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档