簡體   English   中英

上傳的文件沒有保存在 php 的服務器中

[英]Uploaded file doesnt get saved in the server in php

我正在嘗試上傳文件並將文件保存在服務器中,並通過 email 附加和發送文件。通過 email 附加和發送文件可以正常工作,文件沒有保存在服務器的上傳文件夾中。如何將文件保存在服務器中已經通過 email 作為附件發送.. 這是代碼

<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>

電子郵件腳本.php

<?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>';
}

?>

上傳附件時是否收到任何錯誤/警告?

請嘗試在此語句之后調試代碼: if(!empty($_FILES["attachment"]["name"])){ ,就像天氣一樣,您正在 $_FILES 中獲取附件數據。

檢查上傳目錄是否為777(讀、寫、可執行)的權限,如果不是,則應授予該目錄的權限。

要檢查的另一件事是:php.ini 文件中的max_execution_timeupload_max_filesize ,增加它的值

取消鏈接 function 用於刪除服務器上的文件,您可以很好地刪除或注釋掉該部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM