我的PHP中有一段代码处理来自HTML表单的文件上传。如果我上传图像/ txt文件,它可以完美地工作,但我想限制提交只接受zip文件,现在我的代码不起作用。任何人都可以解释为什么?我真的很难过。在下面的代码中,mkdir部分按预期工作。实际上传不起作用。
$valid_formats = array("zip", ".zip", "ZIP", ".ZIP");
$max_file_size = 1024*10000; //100 kb
$job = $_POST['pubCode'];
$client = str_replace(' ', '_', $job);
$datepickerNew = $_POST['datepickerNew'];
$foldername = $datepickerNew . '_' . $client;
$directoryLocation = 'uploads/' . $datepickerNew . '_' . $client . '/';
mkdir('uploads/' . $foldername);
$path = $directoryLocation; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No errors found! Move uploaded files
$newName = str_replace(' ', '_', $name);
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$newName))
$message = "File has been uploaded";
$count++; // Number of successfully uploaded file
}
}
}
}