Php文件上传,检查文件类型

时间:2017-05-06 16:58:01

标签: php file file-upload

我有这个代码,我用来上传多个文件。

    if(isset($_POST['submitButton']))
{
    if(isset($_FILES['gallery']))
    {
        if($_FILES["gallery"]["size"] > 0 )
        {
            foreach($_FILES['gallery']["name"] AS $key=>$file)
            {
                if($_FILES['gallery']['size'][$key]  != 0 )
                {
                    $target_path = "../documents/" . date( "Y-m-d" ) . '-' . rand(10, 999999999) . '-' . $_FILES['gallery']['name'][$key];
                    $fajl_nev = mysqli_real_escape_string($kapcs,  $_POST["images_alt"][ $key ] );                  
                    if(move_uploaded_file( $_FILES['gallery']['tmp_name'][$key], $target_path ))
                    {
                        $file_name = basename($target_path);
                        $sql = 
                        "
                            INSERT INTO letoltheto_fajl
                            (
                                fajl_nev,
                                fajl_file,
                                fajl_datetime
                            )
                            VALUES
                            (
                                '$fajl_nev',
                                '$file_name',
                                NOW()
                            )
                        ";
                        mysqli_query($kapcs, $sql) or die(mysqli_error($kapcs));
                        header("Location: ".$host."/".$admin_folder."/feltoltott-fajlok.php?new=1");
                    }
                    else
                    {
                        $error[] = "A fájl feltöltése nem sikerült, próbálja újra.";
                    }
                }
            }
        }
    }
}

我如何查看,文件类型?我在下面的数组中给出了允许的文件类型:

    $allowed_files = array
(
    "image/jpg", 
    "image/jpeg", 
    "image/bmp", 
    "image/gif",
    "image/png",
    "application/pdf"
);

我还想允许更多的文件,比如excel,word ......我怎么能给出这些类型?有什么内容,我可以在这里读到这个吗?

3 个答案:

答案 0 :(得分:0)

您要检查文件扩展名或mime。尝试这样的事情:

$allowedFiles =  array('gif', 'png', 'jpg', 'jpeg, 'bmp', 'pdf', 'doc', 'docx'); // etc...
$filename = $_FILES['gallery']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext, $allowedFiles) ) {
    echo 'Error...';
}

答案 1 :(得分:0)

txt不在允许列表中,它还允许我上传txt文件。

$allowed_files = array

(     // 图片     'png'=> '图像/ PNG',     'jpe'=> '图像/ JPEG',     'jpeg'=> '图像/ JPEG',     'jpg'=> '图像/ JPEG',     'gif'=> '图像/ GIF',     'bmp'=> '图像/ BMP',

// PDF files
'pdf'  => 'application/pdf',

// Tömörített fájlok
'zip'  => 'application/zip',
'rar'  => 'application/x-rar-compressed',

// MS office
'doc'  => 'application/msword',
'rtf'  => 'application/rtf',
'xls'  => 'application/vnd.ms-excel',
'ppt'  => 'application/vnd.ms-powerpoint',

// Open Office
'odt'  => 'application/vnd.oasis.opendocument.text',
'ods'  => 'application/vnd.oasis.opendocument.spreadsheet'

);

if(isset($_POST['submitButton']))
{
    if(isset($_FILES['gallery']))
    {
        if($_FILES["gallery"]["size"] > 0 )
        {
            foreach($_FILES['gallery']["name"] AS $key=>$file)
            {
                if($_FILES['gallery']['size'][$key]  != 0 )
                {

                    $filename = $_FILES['gallery']['name'][$key];
                    $ext = pathinfo($filename, PATHINFO_EXTENSION);
                    if(!in_array($ext, $allowed_files[0]) ) 
                    {
                        $error[] = "Nem engedélyezett fájl típus.";
                    }

                    $target_path = "../documents/" . date( "Y-m-d" ) . '-' . rand(10, 999999999) . '-' . $_FILES['gallery']['name'][$key];
                    $fajl_nev = mysqli_real_escape_string($kapcs,  $_POST["images_alt"][ $key ] );                  
                    if(move_uploaded_file( $_FILES['gallery']['tmp_name'][$key], $target_path ))
                    {
                        $file_name = basename($target_path);
                        $sql = 
                        "
                            INSERT INTO letoltheto_fajl
                            (
                                fajl_nev,
                                fajl_file,
                                fajl_datetime
                            )
                            VALUES
                            (
                                '$fajl_nev',
                                '$file_name',
                                NOW()
                            )
                        ";
                        mysqli_query($kapcs, $sql) or die(mysqli_error($kapcs));
                        header("Location: ".$host."/".$admin_folder."/feltoltott-fajlok.php?new=1");
                    }
                    else
                    {
                        $error[] = "A fájl feltöltése nem sikerült, próbálja újra.";
                    }
                }
            }
        }
    }
}

答案 2 :(得分:0)

您需要从文件内容而不是客户端模仿类型中获取文件类型。

declare @tab table ( cola varchar(50), colb int, colc varchar(50))

insert into @tab ( cola, colb, colc )
values ( 'A', 10, 'B')
, ( 'C', 20, 'D' )

select name,
case when colname = 'colc' then colb else '0' end as debit,
case when colname = 'cola' then colb else '0' end as credit
FROM @tab S
UNPIVOT
(
    name
    FOR colname  IN (cola,colc)
) U;