1

我一直在尝试编写一个脚本来上传文件,前提是所有选定的文件都匹配预定义的类型(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,这就是为什么我希望所有选定的文件都匹配类型,即轻松编写更新脚本......希望这有任何意义

4

2 回答 2

0

首先将 $HTTP_POST_FILES 更改为 $_FILES

于 2013-07-18T13:30:26.733 回答
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']);
            }

        }
    }
于 2013-07-18T13:50:42.467 回答