附件表格的问题

时间:2014-06-28 09:42:52

标签: php forms conditional-statements attachment

以下代码工作得很好,但是当用户提交没有任何附件时,页面会提供错误警告并且没有任何事情发生,所以我没有任何条件,有人知道我需要添加什么?

<?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'] . "\r\n" . 'שם משפחה: ' . $_POST['sender_suname'] . "\r\n" . 'תאריך לידה: ' . $_POST['sender_Bday'] . "\r\n" . 'עיסוק נוכחי: ' . $_POST['sender_work'] . "\r\n" . 'טלפון: ' . $_POST['sender_phone'] . "\r\n" . 'מייל: ' . $_POST['sender_email'] . "\r\n" . 'טקסט חופשי: ' . $_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.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$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: base64\n\n" . $data . "\n\n";
$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     
} 
  } 

  ?>

2 个答案:

答案 0 :(得分:2)

以下if子句

中的整个代码
if(isset($_FILES) && (bool) $_FILES) {

检查适用的地方。

答案 1 :(得分:0)

请发布错误消息!这使得回答你的问题变得容易多了。

只是阅读代码,一个明显的问题是var $files只在第一个if语句中初始化,但是你在for循环中使用它。

将行$files = array();移到脚本的顶部,使其始终初始化为空数组。