好的,这就是我的做法,我还没有测试过:
编辑:试试这个
//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']);
}
}
}