dqf42223 2018-04-14 20:03
浏览 35

如果使用数组中的值执行空数组

Hi all I have a simple image uploading script for my website with a few checks to be taken before the image is uploaded. If I try to upload for instance a gif or a large file the correct error message will be displayed but the image will still continue to upload. Although this shouldn't be happening as for the error message to be displayed there would have to be a value in the array? Any advice?

<?php
$message = "";
$errors = array();

if (isset($_POST['uploadImg'])) {
// getting data needed from the image
$image = $_FILES['image'];
$imageName = $_FILES['image']['name'];
$imageTmpName = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
$imageError = $_FILES['image']['error'];
$imageType = $_FILES['image']['type'];
$imageInfo = getimagesize($_FILES['image']['tmp_name']);
$imageWidth = $imageInfo[0];
$imageHeight = $imageInfo[1];
// seperates the image name from the image extension
$getImageExt = explode('.', $imageName);
// puts all extensions to lowercase to prevent exceptions e.g. JPEG not     being accepted
$imageExt = strtolower(end($getImageExt));
// array containg the allowed file types
$allowed = array(
    'jpg',
    'jpeg',
    'png',
    'tiff'
);

// checks if the file extension exists in the array
if (!in_array($imageExt, $allowed)) {
$errors[ext] = "Invalid file format";
}



// checks if there are any errors with the image
if (!$imageError === 0) {
$errors[image] = "Error with your file";
}


// checks if the file size is less than 1MB
if (!$imageSize < 1000000) {
     $errors[size] = "File is to big";
}


// check if the image width is less than or equal to 1024px
if (!$imageWidth <= 1024) {
$errors[width] = "Image width is limited to 1024px for layout purposes";
}


// check if image  height is less than or equal to 768 pixels
if (!$imageHeight <= 768) {
$errors[height] = "Image height is limited to 768px for layout purposes";
}

 if (empty($errors)) {

// inserts the uploaded image into the 'images' directory
$imageDestination = 'images/' . $imageName;
move_uploaded_file($imageTmpName, $imageDestination);

} else {

$message = "Something Went Wrong";
}
}
?>
  • 写回答

2条回答 默认 最新

  • dongxiatuo9827 2018-04-14 20:36
    关注

    Your keys in $error aren't wrapped in quotes. eg.

    if (!in_array($imageExt, $allowed)) {
        $errors['ext'] = "Invalid file format";
    }
    // checks if there are any errors with the image
    if (!$imageError === 0) {
        $errors['image'] = "Error with your file";
    }
    // checks if the file size is less than 1MB
    if (!$imageSize < 1000000) {
         $errors['size'] = "File is to big";
    }
    // check if the image width is less than or equal to 1024px
    if (!$imageWidth <= 1024) {
        $errors['width'] = "Image width is limited to 1024px for layout purposes";
    }
    // check if image  height is less than or equal to 768 pixels
    if (!$imageHeight <= 768) {
        $errors['height'] = "Image height is limited to 768px for layout purposes";
    }
    

    That might fix your error.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题