下面的代码工作得很好,但是当用户在没有任何附件的情况下提交时,页面给出错误警报,没有发生任何事情,我错过了任何条件,有人知道我需要添加什么吗?
<?php
if(isset($_FILES) && (bool) $_FILES) {
$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");
$files = array();
foreach($_FILES as $name=>$file) {
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
$file_type = $file['type'];
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions)) {
die("File $file_name has the extensions $ext which is not allowed");
}
array_push($files, array('temp_name' => $temp_name, 'file_name' => $file_name));
}
// email fields: to, from, subject, and so on
$to = "dorozenman@gmail.com";
$from = $_POST['sender_email'];
$subject ="בקשה להצעת עבודה מ" . $_POST['sender_name'] .' '. $_POST['sender_suname'];
$message = 'שם: '. $_POST['sender_name'] . "rn" . 'שם משפחה: ' . $_POST['sender_suname'] . "rn" . 'תאריך לידה: ' . $_POST['sender_Bday'] . "rn" . 'עיסוק נוכחי: ' . $_POST['sender_work'] . "rn" . 'טלפון: ' . $_POST['sender_phone'] . "rn" . 'מייל: ' . $_POST['sender_email'] . "rn" . 'טקסט חופשי: ' . $_POST['sender_way'] ;
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"";
// multipart boundary
$message = "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $message . "nn";
$message .= "--{$mime_boundary}n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$temp_name = $files[$x]['temp_name']; // temporary file location
$file_name = $files[$x]['file_name']; // filename
$file = fopen($temp_name,"rb");
$data = fread($file,filesize($temp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {"application/octet-stream"};n" . " name="$file_name"n" .
"Content-Disposition: attachment;n" . " filename="$file_name"n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
$message .= "--{$mime_boundary}n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) { ?>
<script language="javascript" type="text/javascript">
alert('הבקשה נשלחה! שיהיה לך בהצלחה!');
top.window.location = 'https://www.facebook.com/gingereilat';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('שגיאה במערכת בצעו פעולה זאת מאוחר יותר או צרו קשר dorozenman@gmail.com');
top.window.location = 'https://www.facebook.com/gingereilat';
</script>
<?php
}
}
?>
if子句后面的整个代码
if(isset($_FILES) && (bool) $_FILES) {
请发布错误消息!这样回答你的问题就容易多了。
仅阅读代码,一个明显的问题是var $files
仅在第一个if
语句中初始化,但随后您在for
循环中使用它。
将$files = array();
行移动到脚本的顶部,使其始终初始化为空数组。