1 回答
TA贡献1784条经验 获得超7个赞
我已经复制并以某种方式测试了您的代码。您不需要执行 is_uploaded_file 调用,因为这些检查是在 move_uploaded_file 函数中进行的(https://www.php.net/manual/en/function.is-uploaded- file.php#113766)。另请检查存储图像的目录是否存在。
<?php
/* Directory where uploaded images will be placed */
$imageDir = 'img/articles';
/* Checking request parameters */
if (!isset($_FILES['articleImage']) || empty($_FILES['articleImage'])) {
echo "Error : the file is missing";
die();
}
/* Checking upload errors */
if ($_FILES['articleImage']['error'] != UPLOAD_ERR_OK) {
echo "Error : " . $_FILES['articleImage']['error'];
die();
}
/* Checking file size */
if ($_FILES['articleImage']['size'] == 0) {
echo "Error : the file is empty";
die();
}
/* Checking if directory exists */
if (!is_dir($imageDir)) {
echo 'Image directory does not exist';
die();
}
/* Checking file doesn't already exist */
$imagePath = $imageDir . $_FILES['articleImage']['name'];
if (file_exists($imagePath)) {
echo "Error : the filename is already taken";
die();
}
/* Writing image */
if (!move_uploaded_file($_FILES['articleImage']['tmp_name'], $imagePath)) {
echo "Error : the file coundn't be written";
die();
}
echo 'Success';
die();
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报