I'm having a problem with php approving my file upload. I want the user to only upload .xml files. But it doesn't work.
我在php批准我的文件上传时遇到了问题。我希望用户只上传.xml文件。但这并不管用。
Here is my html form:
下面是我的HTML表单:
<form action="upload2.php" method="post" enctype="multipart/form-data">
Wähle deine Sprachdatei aus:
<input type="file" class="form-control-file" name="upfile">
<br>
<input type="submit" class="btn btn-primary" value="Sprachdatei hochladen" name="submit">
</form>
and here is my php to control the file via MIME type:
下面是我通过MIME类型控制文件的php:
<?php
header('Content-Type: text/plain; charset=utf-8');
try {
// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if (
!isset($_FILES['upfile']['error']) ||
is_array($_FILES['upfile']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
// Check $_FILES['upfile']['error'] value.
switch ($_FILES['upfile']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
// You should also check filesize here.
if ($_FILES['upfile']['size'] > 1000000) {
throw new RuntimeException('Exceeded filesize limit.');
}
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'xml' => 'text/xml',
'txt' => 'text/plain',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
// You should name it uniquely.
// DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
// On this example, obtain safe unique name from its binary data.
if (!move_uploaded_file(
$_FILES['upfile']['tmp_name'],
sprintf('./uploads/%s.%s',
sha1_file($_FILES['upfile']['tmp_name']),
$ext
)
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
echo 'File is uploaded successfully.';
} catch (RuntimeException $e) {
echo $e->getMessage();
}
?>
with simple jpg and png it works but not with xml.
I checked the MIME Type and it's still not working
对于简单的jpg和png,它可以工作,但不能使用XML。我检查了MIME类型,但它仍然不起作用
I'm using XAMPP on Windows to run php
我在Windows上使用XAMPP来运行php
更多回答
Have you checked what $finfo->file($_FILES['upfile']['tmp_name'])
actually returns?
你有没有检查过$finfo->file($_FILES-“upfile”]“tmp_name”])实际返回的是什么?
Sorry. I'm a php lerner. How do I check it?
抱歉的。我是Php Lerner。我该怎么检查呢?
优秀答案推荐
It seems like $finfo->file()
returns application/xml
instead of text/xml
for xml files.
对于XML文件,$finfo->file()似乎返回的是应用程序/XML,而不是文本/XML。
Change your array with valid mime-types to this:
将具有有效MIME类型的数组更改为:
array(
'xml' => 'application/xml',
'txt' => 'text/plain',
),
This worked for me.
这对我很管用。
'xml' => ['text/xml', 'application/xml'],
'txt' => 'text/plain',
Try with the below code. Extension of the file was getting properly on your code.
尝试使用以下代码。文件扩展名在您的代码中得到了正确的处理。
if (!move_uploaded_file(
$_FILES['upfile']['tmp_name'], 'uploads/'.basename($_FILES["upfile"]["name"]
)
))
IF(!MOVE_UPLOADED_FILE($_FILES[‘UPFILE’][‘tMP_NAME’],‘UPLOADS/’.basename($_FILES[“UPFILE”][“NAME”]))
更多回答
How is this related to his question?
这与他的问题有什么关系?
When I had checked on my local machine, file was uploading without extension.
当我在本地机器上检查时,文件正在上传,没有扩展名。
That's fine. But that wasn't his question. He wanted to restrict uploading on MIME-type. Anyway, he got an answer that worked for him.
那很好。但这不是他的问题。他想限制MIME类型的上传。无论如何,他得到了一个对他有效的答案。
我是一名优秀的程序员,十分优秀!