gpt4 book ai didi

php - 如何验证文件上传字段?

转载 作者:可可西里 更新时间:2023-10-31 22:59:44 26 4
gpt4 key购买 nike

我似乎无法验证文件上传字段,这是我的代码:

<?php

if (isset($_POST['submit'])) {
if ($_POST['uploadFile']) {
echo "File uploaded";
} else {
echo "No file attempted to be uploaded";
}
}

?>

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" />
<input type="submit" name="submit" />
</form>

我犯了什么错误?

编辑

想出了我自己的解决方案,可能不是最好的,但它有效:

if (isset($_POST['submit'])) {
$fileUpload = $_FILES['uploadFile'] ['name'];
if (strlen($fileUpload) > 0) {
echo "File uploaded.";
} else {
echo "No File attempted to be uploaded.";
}
}

最佳答案

您正在寻找的超全局变量是 $_FILES。

var_dump($_FILES); //to see what's happening with the uploaded files

要确保发出 POST 请求,您可以使用以下行:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
//A post request!
}

关于php - 如何验证文件上传字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com