2016-11-29 21 views
-2

ここでもPHPのハングを取得しています。私はこれに似た他の質問を見たことがあるが、私がなぜ偽を返すのか理解できない。テキストを送信し、電子メールでファイルを添付するフォームがあります。フォームはファイルを期待どおりに送信しますが、一度ファイルタイプの検証を開始するとエラーが発生するだけです。なぜ私の検証は常にfalseを返すのですか?in_array()が常にfalseを返すのはなぜですか? PHP

$error = False; 
$file_type = $_FILES['upload']['type']; //returns the mimetype 

$allowed = array("image/jpeg", "image/png", "image/gif", "application/pdf"); 


$total = count($_FILES['upload']['name']); 
$attachments = "None"; 
$attachmentString = "No Attachments"; 

if(isset($_FILES['upload']['name']) && is_array($_FILES['upload']['name']) && count($_FILES['upload']['name']) > 0){ 

    // Loop through each file 
    for($i=0; $i<$total; $i++) { 
     //Get the temp file path 
     $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; 

     //Make sure we have a filepath 
     if ($tmpFilePath != ""){ 
     $uploaddir = trailingslashit(realpath(__DIR__)) . 'uploads/'; 
     wp_mkdir_p($uploaddir); 
     //Setup our new file path 
     $newFilePath = $uploaddir . basename($_FILES['upload']['name'][$i]); 
     if(!in_array($file_type, $allowed)) { 
      $error = True; 
      echo "Error: Only jpg, gif, png, and pdf files are allowed.<br />"; 
     } 
     else 
     { 
      //Upload the file into the temp dir 
      if(move_uploaded_file($tmpFilePath, $newFilePath)) { 
       $attachments[$i] = $newFilePath; 
       //make string for mimetype headers in email 
       $attachmentString += implode($attachments[$i]) + ", "; 
      } 
     } 

    } 
} 

}

+2

。 – Phiter

+1

'$ file_type'は__array__です。そして、あなたがそれを印刷したら、あなたはそれを見るでしょう。しかし、今日誰がデバッグしていますか? –

+0

in_arrayがあなたに与える前にecho $ file_typeとは何ですか?また、あなたのファイルタイプ配列 – dpp

答えて

2

あなたがPHPで複数のファイルを更新すると、各ファイルのmimetype$_FILES['userfile']['type'][$i]変数になります。

コードで$_FILES['userfile']['type'](配列)の値が$allowedの型に存在する(これは常にfalseです)かどうかをチェックします。

あなたは、あなたにコードを変更することができます: `のvar_dump($ FILE_TYPE)`を試してみて、それが正しいかどう見る

for($i=0; $i<$total; $i++) { 
    .... 
    $file_type = $_FILES['upload']['type'][$i]; //returns the mimetype 
    .... 
    if(!in_array($file_type, $allowed)) { 
     $error = True; 
     echo "Error: Only jpg, gif, png, and pdf files are allowed.<br />"; 
    } 
    .... 
} 
+0

ありがとうございました! –

関連する問題