dongtan8122 2019-02-05 03:17
浏览 16

使用输入字段上载文件

I have included the file type check it the below code. If i select 2 files from allowed type and one file from not allowed type the code still save the allowed one and show the msg for the file type as well. I want if any file is other than the allowed one the code should not save any thing.

<?php
// Script written by Adam Khoury for the following video exercise:
// http://www.youtube.com/watch?v=7fTsf80RJ5w
if(isset($_FILES['file_array'])) {
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];

    for($i = 0; $i < count($tmp_name_array); $i++){

        $fileext [$i] = explode('.', $name_array[$i]);
        $fileactualext[$i] = strtolower(end($fileext[$i]));
        $allowed = array("jpg","jpeg", "pdf");

        if (in_array($fileactualext[$i], $allowed)) {
            if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])) {
                    echo $name_array[$i]."upload is complete<br>";
            } else {
                    echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
                    }

        } else {
                header('location:file_type.php');
                }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dsm1998 2019-02-05 05:02
    关注

    Use the following code to check for image size, type, error,

    $file_ext=strtolower(end(explode('.',$_FILES['file_array']['name'])));
    
      $extensions= array("jpeg","jpg","png");
    
    if(in_array($file_ext,$extensions)=== false){
            $errors[]="extension not allowed, please choose a JPEG or PNG file.";
     }
    
     if($file_size > 2097152){ // set whatever max size you want
           $errors[]='File size must be excately 2 MB';
     }
    
     if(empty($errors)==true){
           move_uploaded_file($file_tmp,"images/".$file_name);
           echo "Success";
     }else{
          print_r($errors);
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据