您的代码未解决的问题是显示多个错误。如上所述,用户可能会上传错误类型> 2MB的文件,但是您的代码只能报告其中一个问题。尝试类似的方法:
if(isset($_FILES['uploaded_file'])) { $errors = array(); $maxsize = 2097152; $acceptable = array( 'application/pdf', 'image/jpeg', 'image/jpg', 'image/gif', 'image/png' ); if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) { $errors[] = 'File too large. File must be less than 2 megabytes.'; } if((!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) { $errors[] = 'Invalid file type. only PDF, JPG, GIF and PNG types are accepted.'; } if(count($errors) === 0) { move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file'); } else { foreach($errors as $error) { echo '<script>alert("'.$error.'");</script>'; } die(); //Ensure no more processing is done }}move_uploaded_file()有关更多信息,请参阅文档(称为“移动不存储”)。



