这是我在Stack Overflow上的第一篇文章,所以请耐心等待 - 我已经使用了帖子,因为我无法通过Google / Stack Overflow找到答案。
我是PHP新手,学习如何添加文件上传。我有一个非常基本的html表单,通向PHP页面。
<form action="UploadPage.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000"/>
<input type="file" name="uploadedXML"/>
<input type="submit" value="Upload"/>
处理文件上载的PHP代码有一系列if语句来检查文件是否是正确的类型大小等。如果有错误,则会在错误页面上生成相应的错误消息。
我一直在测试上传各种文件类型,以确定错误语句是否正确发生,我遇到第二个问题(检查文件类型)和第三个(检查文件大小)。
如果文件类型检查if语句是否排在第一位,我发现如果我上传的XML文件大于最大大小(100kb),我仍然会收到与文件类型检查有关的错误消息 - 当我收到错误消息时与文件大小有关。
但是,如果我交换IF语句,所以文件大小检查在文件类型检查之前,如果我上传不正确的文件类型但是大小合适(例如一个小图像)我得到一个关于文件太大的错误消息,当我期待一个与文件类型有关的错误时。
<?php
const UploadKey = 'uploadedXML';
const AllowedTypes = ['application/xml','text/xml'];
session_start();
/*Checking for errors*/
if (empty($_FILES[UploadKey]['name'])){//check file actually been uploaded
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="You forgot to add your file!";
die();
}
if (!in_array($_FILES[UploadKey]['type'],AllowedTypes)){//Check correct type of file
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="We only accept XML files I'm afraid";
die();
}
if ($_FILES[UploadKey]['error'] == 2) {//Check if size too big
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="Your file is too big for us to handle, awkward! Please choose a file under 100KB.";
die();
}
$tempFileLoc = $_FILES[UploadKey]['tmp_name'];
$destFileLoc = 'Uploads/'.$_FILES[UploadKey]['name'];
if (file_exists($destFileLoc)) {// Check if file already exists
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="We've already got this file, thanks though";
die();
}
if ($_FILES[UploadKey]['error']>0){
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="Unfortunately there's been an error with the uploading process";
die();
}
如果您需要查看我的代码以帮助回答,请告诉我。
首先十分感谢!
最佳做法是构建一个错误数组,如果它是空的继续下一步或如果不返回错误。你可以尝试这样的事情。在您的代码中,您覆盖了错误消息,因此您只看到最后一个应用,而不是所有上传的消息都可能已触发。
<?php
const UploadKey = 'uploadedXML';
const AllowedTypes = ['application/xml','text/xml'];
$errors = array();
session_start();
/*Checking for errors*/
if (empty($_FILES[UploadKey]['name'])){//check file actually been uploaded
$errors[] = "You forgot to add your file!";
}
if (!in_array($_FILES[UploadKey]['type'],AllowedTypes)){//Check correct type of file
$errors[] ="We only accept XML files I'm afraid";
}
if ($_FILES[UploadKey]['error'] == 2) {//Check if size too big
$errors[] ="Your file is too big for us to handle, awkward! Please choose a file under 100KB.";
}
$tempFileLoc = $_FILES[UploadKey]['tmp_name'];
$destFileLoc = 'Uploads/'.$_FILES[UploadKey]['name'];
if (file_exists($destFileLoc)) {// Check if file already exists
$errors[] ="We've already got this file, thanks though";
}
if ($_FILES[UploadKey]['error'] > 0){
$errors[] = "Unfortunately there's been an error with the uploading process";
}
//if errors were found
if(!empty($error)){
header("Location: ErrorPage.php");
//beware this is now an array and not a single string
$_SESSION['errorMessage']= $errors;
die();
}
该问题是由于您在HTML表单中包含的MAX_FILE_SIZE
导致的。
如果您上传的文件超过了表单中设置的MAX_FILE_SIZE
,PHP会自动清空tmp_name
和type
,并将size
变为0(文件为$ _FILES)。
因此$_FILES[UploadKey]['type']
为空,因此您用来检查文件类型是否允许的条件将返回false。
要纠正这个问题,你还应该检查以确保类型不是空的
if (!empty($_FILES[UploadKey]['type']) && !in_array($_FILES[UploadKey]['type'],AllowedTypes)
像这样的东西:
<?php
if (!empty($_FILES[UploadKey]['type']) && !in_array($_FILES[UploadKey]['type'],AllowedTypes)){// Make sure the file type is not empty
header("Location: ErrorPage.php");
$_SESSION['errorMessage']="We only accept XML files I'm afraid";
die();
}