【发布时间】:2020-02-18 05:24:39
【问题描述】:
我正在尝试上传图片。我已经包含了来自不同页面 (inc/pages/addRecipe2.php) 的表单,并且表单加载正常,我在同一页面中运行表单的代码 (addRecipe/?token=...),当我尝试上传文件说有一个未定义的索引:图片
表格代码:
<form enctype="multipart/file-data" method="POST">
<div class="form-group">
<input type="file" class="form-control-file" id="image" name="image">
</div>
<?php
if ($privateRecipe == "public") {
echo "<button type='submit' class='btn btn-primary' style='width: 100%' id='finishRecipe' name='finishRecipe'>Submit recipe for review</button>";
} else {
echo "<button type='submit' class='btn btn-primary' style='width: 100%' id='finishRecipe' name='finishRecipe'>Complete reci</button>";
}
?>
<p class="formMessage1"></p>
</form>
php代码,运行表单:
if (isset($_POST['finishRecipe'])) {
$recipeId = $_GET['token'];
$image = $_FILES['image'];
$imageName = $_FILES['image']['name'];
$imageTmpName = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
$imageError = $_FILES['image']['error'];
$imageType = $_FILES['image']['type'];
$fileExt = explode(".", $imageName);
$fileActualExt = strtolower(end($fileExt));
$allowedExt = array('png', 'jpg', 'jpeg');
if (in_array($fileActualExt, $allowedExt)) {
if ($imageError === 0) {
if ($imageSize < 500000) {
$fileNameNew = uniqid('', true) . "." . $fileActualExt;
$fileDestination = "uploads/$fileNameNew";
move_uploaded_file($imageTmpName, $fileDestination);
} else {
echo "The image is too big!";
}
} else {
echo "There was an error with your image!";
}
} else {
echo "File type not allowed!";
}
}
它确实将“文件类型不允许”作为我制作的错误消息之一回显。
有什么解决办法吗?
【问题讨论】:
-
你上传的文件扩展名不是'png'、'jpg'或'jpeg'?
$allowedExt = array('png', 'jpg', 'jpeg'); -
strtolower( end( $fileExt ) )的值是多少 -
我已经用我得到的错误更新了帖子 - 在图像中
-
更改表单 enctype="multipart/form-data">