Home Backend Development PHP Tutorial php 上传文件 判断 文件类型解决思路

php 上传文件 判断 文件类型解决思路

Jun 13, 2016 pm 01:46 PM
break case quot

php 上传文件 判断 文件类型
我 现在 想 上传 一个 csv 文件,
在 php 里 怎么 判断 这个 文件 类型 是 csv 的呢?

注:
除了 判断 扩展名 之外,还有 什么 判段 的 方法??

------解决方案--------------------
$_FILES["file"]["type"]
------解决方案--------------------

探讨

$_FILES["file"]["type"]

------解决方案--------------------
学习了!!!!!!!!!!!!!!!
------解决方案--------------------
楼主非要把简单的事情复杂化。那也没办法!
------解决方案--------------------
网上流传的一个不错的方法
PHP code
function ftype($filename){
    $file    = fopen($filename, "rb");
    $bin    = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo    = @unpack("C2chars", $bin);
    $typeCode    = intval($strInfo['chars1'].$strInfo['chars2']);
    $fileType    = '';
    switch($typeCode){
        case 7790:
            $fileType = 'exe';
            break;
        case 7784:
            $fileType = 'midi';
            break;
        case 8297:
            $fileType = 'rar';
            break;
        case 255216:
            $fileType = 'jpg';
            break;
        case 7173:
            $fileType = 'gif';
            break;
        case 6677:
            $fileType = 'bmp';
            break;
        case 13780:
            $fileType = 'png';
            break;
        default:
            $fileType = 'unknown';
    }
    return $fileType;
}
<br><font color="#e78608">------解决方案--------------------</font><br>我认为8楼得方法已经尽最大程度判定了,按照楼主的意思就是上传的文件如何判定其内容是不是csv格式的(不相信扩展名),那就只能获取文件里面内容,并根据里面内容的形式判定是不是csv文件,但是这种办法是要在后台实现的,也就是说此时文件已经上传了,上传了再判定就没有什么用了。所以,个人认为,不上传,还要判断内容形式,没什么好的解决办法。
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

我的 意思是 csv 不是 用 , 这个 隔开的 文件 吗

aa,bb,cc,dd
ee,ff,gg,hh

类似 这样的 文件
按 8楼 的 说法 这个 文件 的 typeCode=131112
可是

aa,"b
b",cc,dd
ee,ff,gg,hh

这个 样子 也 是正确 的 csv 格式 类型 但是 这个 文件 的 typeCode=105105
……
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

HMD Skyline gets a new color option and official magnetic case HMD Skyline gets a new color option and official magnetic case Aug 23, 2024 am 07:04 AM

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

switch case judgment variable switch case judgment variable Feb 19, 2024 am 08:04 AM

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values ​​​​of variables: #includeintmain(){

What is the use of break stop statement in Go language? What is the use of break stop statement in Go language? Jan 18, 2023 pm 03:46 PM

In the Go language, the break stop statement is used to jump out of the loop in a loop statement and start executing the statement after the loop. The break statement can end the code blocks of for, switch and select. In addition, the break statement can also add a label after the statement to indicate exiting the code block corresponding to a certain label. The label requirement must be defined on the corresponding code block of for, switch and select. .

JS loop learning: break out of loop statements break and continue JS loop learning: break out of loop statements break and continue Aug 03, 2022 pm 07:08 PM

In the previous article, we took you to learn several loop control structures in JS (while and do-while loops, for loops). Let’s talk about the break and continue statements to jump out of the loop. I hope it will be helpful to everyone!

What is the usage of break in php What is the usage of break in php Jan 31, 2023 pm 07:33 PM

In PHP, break is used to jump out of the current syntax structure and execute the following statements; it can be used in statements such as switch, for, while, and do while. It can terminate the code of the loop body and jump out of the current loop immediately, and execute the following statements after the loop. code. The break statement can take a parameter n, which represents the number of levels to jump out of the loop. If you want to jump out of multiple loops, you can use n to represent the number of levels to jump out of. If there is no parameter, the default is to jump out of the current loop.

Let's talk about not using break in PHP switch statement Let's talk about not using break in PHP switch statement Mar 20, 2023 pm 04:55 PM

It is very common to use switch statements to select multiple branches in PHP. Usually, a break statement is used to exit the switch statement after each branch. However, there are situations where we do not want to use the break statement. This article will introduce the situation of not using break in the PHP switch statement.

How to eliminate switch-case in Springboot How to eliminate switch-case in Springboot May 14, 2023 pm 07:49 PM

基本逻辑如下:Stringevent=crsRequest.getEvent();CRSResponsecrsResponse=null;switch(event){caseCRSRequestEvent.APP_START:crsResponse=processAppStartCommand(crsRequest);break;caseCRSRequestEvent.INIT_COMPLETE:crsResponse=processInitCompleteCommand(crsRequest)

See all articles