如果所有文件都符合条件,PHP上传文件检查循环

时间:2013-07-18 13:19:17

标签: php file-upload for-loop

我一直在尝试编写脚本以仅在所有选定文件与预定义类型(pdf,doc或docx)匹配时才上传文件。例如,如果用户选择3个文件,则所有文件都需要符合条件,否则将不会上传所选文件。

这是我到目前为止所做的:

<?php

for($i=0; $i<count($_FILES['file']['name']); $i++) {
$file_type = $_FILES['file']['type'][$i];   
$allowedExts = array("application/pdf", "application/msword",  "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

if(in_array($file_type, $allowedExts)) 
{

  if ($_FILES["file"]["error"][$i] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br>";
}
else
{
if (file_exists("uuuuu/" . $_FILES["file"]["name"][$i]))
  {

  $file_name = $HTTP_POST_FILES['file']['name'][$i];
  $random_digit=rand(0000,9999);

  $new_file_name=$random_digit.$file_name;
  $file_final = str_replace(' ', '_', $new_file_name);
  $path= "uuuuu/".$file_final;

  copy($HTTP_POST_FILES['file']['tmp_name'][$i], $path);
  $message = "success...."; 
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"][$i],
  "uuuuu/" . $_FILES["file"]["name"][$i]);
  $message = "success...."; 
  }
}
  }

  else
  {   
  $message1 ="at least one of your files has the wrong extension! you can only select doc,docx or pdf";

  }

  }

?>

<form id="form2" name="form2" method="post" action="" enctype="multipart/form-data">
<input name="file[]" type="file" id="file" class="for-text-fields" multiple="multiple"/>
  <label for="textarea2"></label>
  <textarea name="textarea2" id="textarea2" rows="10" class="for-text-f"></textarea>
  <br /><br />
  <input name="submit" type="submit" id="submit" value="submit" />
</form>

我知道这应该不难做到,但是我无法理清:(我真的很乐意帮助你解决这个问题。

编辑:我忘了提到将有4个文本字段,用户将为每个选定的文件键入一些标题,无论是1,2,3还是4,这就是为什么我希望所有选定的文件与类型匹配,即轻松编写更新脚本...希望这有任何意义

2 个答案:

答案 0 :(得分:0)

将$ HTTP_POST_FILES更改为$ _FILES作为开始

答案 1 :(得分:0)

好的,我就是这样做的,我还没有测试过:

编辑:尝试这个

//list acceptable types
$allowedExts = array("application/pdf", "application/msword",     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

//loop through uploaded files
for($i=0; $i<count($_FILES['file']['name']); $i++) {

    $file_type = $_FILES['file']['type'][$i];   

    if(in_array($file_type, $allowedExts)) {
        if ($_FILES["file"]["error"][$i] > 0) {
          $errors[] = "Return Code: " . $_FILES["file"]["error"][$i] . "<br>";
        }else {
            //if duplicate file add to array to change name
            if (file_exists("uuuuu/" . $_FILES["file"]["name"][$i])) {
                    $duplicates[$i]['name'] = $_FILES['file']['name'][$i];
                $duplicates[$i]['tmp_name'] = $_FILES['file']['tmp_name'][$i];       
              }else {
                  //it's correct file format, a file by the same name doesn't already exist & there's no file error. 
                  $eligible[$i]['tmp_name'] = $_FILES["file"]["tmp_name"][$i]; 
                  $eligible[$i]['name']     = $_FILES['file']['name'][$i]; 
              }
        }
     }else{
        $errors[] = 'Incorrect file format for '.$_FILES['file']['name'][$i];
    }
}

if($duplicates) {
     array_filter($duplicates);
}

//we will now have an array of possible errors / files that are ok but need name changing / elligible uploads 

//first check for errors - which would include incorrect file types which you state you don't want any 
if($errors) {
    die( print_r($errors) ); //could make this nicer 
}else {
  //no errors so process file name changes if any then upload all files 
    if($duplicates) {
        for($i=0;$i<count($duplicates);$i++) {
             $file_name = $duplicates[$i]['name'];
                 $random_digit=rand(0000,9999);
             $new_file_name=$random_digit.$file_name;
             $file_final = str_replace(' ', '_', $new_file_name);
             $path= "uuuuu/".$file_final;
             copy($duplicates[$i]['tmp_name'], $path);
             $message = "success....";  
        }
    }
    //upload remaining elibigle 
    for($i=0;$i<count($eligible);$i++) {
            move_uploaded_file($eligible[$i]['tmp_name'], "uuuuu/" .         $eligible[$i]['name']);
    }

}

新编辑:这为我工作..

     if($_FILES) {

        //list acceptable types
        $allowedExts = array("application/pdf", "application/msword",     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

        //loop through uploaded files
        for($i=0; $i<count($_FILES['file']['name']); $i++) {

            $file_type = $_FILES['file']['type'][$i];   

            if(in_array($file_type, $allowedExts)) {
                if ($_FILES["file"]["error"][$i] > 0) {
                 $errors[] = "Return Code: " . $_FILES["file"]["error"][$i] . "<br>";
                }else {
                    //if duplicate file add to array to change name
                    if (file_exists("uuuuu/" . $_FILES["file"]["name"][$i])) {
                            $duplicates[$i]['name'] = $_FILES['file']['name'][$i];
                        $duplicates[$i]['tmp_name'] = $_FILES['file']['tmp_name'][$i];       
                      }else {
                          //it's correct file format, a file by the same name doesn't already exist & there's no file error. 
                          $eligible[$i]['tmp_name'] = $_FILES["file"]["tmp_name"][$i]; 
                          $eligible[$i]['name']     = $_FILES['file']['name'][$i]; 
                      }
                }
             }else{
                $errors[] = 'Incorrect file format for '.$_FILES['file']['name'][$i];
            }
        }

        if($duplicates) {
            array_filter($duplicates);
        }

        //we will now have an array of possible errors / files that are ok but need name changing / elligible uploads 

        //first check for errors - which would include incorrect file types which you state you don't want any 
        if($errors) {
            die( print_r($errors) ); //could make this nicer 
        }else {
          //no errors so process file name changes if any then upload all files 
            if($duplicates) {
                for($i=0;$i<count($duplicates);$i++) {
                     $file_name = $duplicates[$i]['name'];
                    $random_digit=rand(0000,9999);
                     $new_file_name=$random_digit.$file_name;
                     $file_final = str_replace(' ', '_', $new_file_name);
                     $path= "uuuuu/".$file_final;
                     move_uploaded_file($duplicates[$i]['tmp_name'], "uuuuu/" .         $new_file_name);
                }
            }
            //upload remaining elibigle 
            for($i=0;$i<count($eligible);$i++) {
                    move_uploaded_file($eligible[$i]['tmp_name'], "uuuuu/" .         $eligible[$i]['name']);
            }

        }
    }