上载的文件无法保存在php中的服务器中
我正在尝试上传一个文件,并将文件保存在服务器以及attac中,然后通过电子邮件发送文件。。通过电子邮件附加和发送文件可以正常工作,但文件不会保存在服务器的上载文件夹中。。如何将文件保存在服务器中,并通过电子邮件作为附件发送。。这是密码上载的文件无法保存在php中的服务器中,php,Php,我正在尝试上传一个文件,并将文件保存在服务器以及attac中,然后通过电子邮件发送文件。。通过电子邮件附加和发送文件可以正常工作,但文件不会保存在服务器的上载文件夹中。。如何将文件保存在服务器中,并通过电子邮件作为附件发送。。这是密码 <form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm"
<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
<?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>';
}
?>
上传附件时是否收到任何错误/警告 请尝试在此语句之后调试代码:如果(!empty($\u FILES[“attachment”][“name”]){,就像您是否在$\u文件中获取附件数据一样 检查上传目录的权限是否为777(读、写、可执行),如果不是,则应授予该目录权限
还有一件事需要检查:max\u execution\u time和上传\u max\u filesize在php.ini文件中,增加它的值上传附件时是否收到任何错误/警告 请尝试在此语句之后调试代码:如果(!empty($\u FILES[“attachment”][“name”]){,就像您是否在$\u文件中获取附件数据一样 检查上传目录的权限是否为777(读、写、可执行),如果不是,则应授予该目录权限
还有一件事需要检查:max\u execution\u time和上传php.ini文件中的max\u filesize,增加它的值取消链接功能用于删除服务器上的文件,您可以很好地删除或注释掉该部分。取消链接功能用于删除服务器上的文件,您可以做得很好l删除或注释该部分。您收到抱歉消息了吗?没有。抱歉消息不会显示。但是我收到的带有附件的电子邮件
@unlink($uploadedFile)
在发送电子邮件后删除了上载的文件。仅供参考,您可以使用$data=file\u get\u内容($uploadedFile)一步读取文件
如果我评论@unlink($uploadedFile)文件保存在服务器中,邮件也会出现?你收到抱歉消息了吗?没有。抱歉消息不会显示..但是我收到的带有附件的电子邮件@unlink($uploadedFile)
将在上传的文件发送电子邮件后删除该文件。仅供参考,如果我在@unlink($uploadedFile)上发表评论,您可以使用$data=file\u get\u contents($uploadedFile)
一步读取文件文件保存在服务器中,邮件也会发送?问题是文件是通过电子邮件发送的。因此,这些都不是问题。问题是文件是通过电子邮件发送的。因此,这些都不是问题。