Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件类型检查不起作用_Php - Fatal编程技术网

Php 文件类型检查不起作用

Php 文件类型检查不起作用,php,Php,在服务器上上载时,我的文件类型检查有问题。我的职能没有发挥应有的作用。在服务器上总是绝对上传所有内容。请帮帮我 <?php session_start(); include_once 'dbconnect.php'; if (isset($_POST['ulozitzmeny'])) { $valid_mime_types = array( "image/gif", "image/png", "image/jpg",

在服务器上上载时,我的文件类型检查有问题。我的职能没有发挥应有的作用。在服务器上总是绝对上传所有内容。请帮帮我

<?php
session_start();
include_once 'dbconnect.php';

if (isset($_POST['ulozitzmeny'])) {

    $valid_mime_types = array(
        "image/gif",
        "image/png",
        "image/jpg",
        "image/jpeg",
    );

    if (in_array($_FILES["file"]["type"], $valid_mime_types)) {

        $file = rand(1000, 100000) . "-" . $_FILES['file']['name'];
        $file_loc = $_FILES['file']['tmp_name'];
        $file_size = $_FILES['file']['size'];
        $file_type = $_FILES['file']['type'];
        $folder = "images";

        $new_size = $file_size / 1024;
        $new_file_name = strtolower($file);
        $final_file = str_replace(' ', '-', $new_file_name);

        if (move_uploaded_file($file_loc, $folder . $final_file)) {
            $sql = "UPDATE users SET file='$file', type='$file_type', size='$file_size' WHERE username = '$_SESSION[user]'";
            mysql_query($sql);
        }
    }else{

        echo 'error';
    }
}
?>

有一种更简单的方法来验证上传的文件类型。使用
fileinfo
获取上载文件的扩展名,然后与允许的文件扩展名进行比较

以下是参考资料:

您的代码应该如下所示:

// your code

// valid file extensions
$valid_extensions = array("gif", "png", "jpg", "jpeg");

// get the file extension
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));  // png

// now check against permissible extensions
if(in_array($ext, $valid_extensions)){
    // allowed
}else{
    // not allowed
}

// your code

您的html标记表单是这样的吗?有时enctype标记丢失。是的,我的html标记表单如下所示。如果您试图处理的数据正确,请检查$\u文件和$\u POST的输出