Home  >  Article  >  Backend Development  >  Detailed explanation of method example of PHP judging whether file has been uploaded

Detailed explanation of method example of PHP judging whether file has been uploaded

伊谢尔伦
伊谢尔伦Original
2017-06-27 14:04:185054browse

A qualified programmer will have some very strict filtering and data rules when implementing data into the database. For example, when we file upload, we must determine whether the user chooses to upload a file in the early stage. At the same time, you can also determine whether there are uploaded files in the background. The example in this article will make a more in-depth analysis of this.

The following html code is shown:

<form action="?" method="post" enctype=&#39;multipart/form-data&#39;>
文件上传:<input type="file" name="file" id="file"/>
<input type="submit" id="send" value="提交"/>
</form>

We most commonly use simple judgments on the front end

<script>
var send=document.getElementById("send");
send.onclick=function(){
var file=document.getElementById("file").value;
if(file.length<1){
alert(&#39;请选择图片&#39;);
return false;
}
}
</script>

If we want to make real securityWe need to enter judgment processing in the background

<?php
//判断pic文件框是否已经选择文件
if(!empty($_FILES[&#39;file&#39;][&#39;tmp_name&#39;])){
echo&#39;已选择文件&#39;;
}else{
echo&#39;请选择文件&#39;;
}
//PS:$_FILES后面的[&#39;tmp_name&#39;]一定不要忘写,它表示是一个临时的意思
?>

An example analysis

js judgment is relatively general. We just used file=document.getElementById("file") .value; to determine whether the file has a value or is not empty, so that you can submit it directly by entering a number, so we need to enter the user name limit for uploading files
such as

function CheckWorkFile()
{
var obj=document.getElementById(&#39;fuMain&#39;);
if(obj.value==&#39;&#39;)
{
alert(&#39;请选择要上传的作业书文件&#39;);
return false;
}
var stuff=obj.value.match(/^(.*)(\.)(.{1,8})$/)[3];
if(stuff!=&#39;doc&#39;)
{
alert(&#39;文件类型不正确,请选择.doc文件&#39;);
return false;
}
return true;
}


For php processing, we only use if(!empty($_FILES['file']['tmp_name'])){ to judge that it is not empty. In fact, this is also unreasonable.
If we can handle it like this

function file_type($filename)
{
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);
    $typeCode = intval($strInfo[&#39;chars1&#39;].$strInfo[&#39;chars2&#39;]);
    $fileType = &#39;&#39;;
    switch ($typeCode)
    {
        case 7790:
            $fileType = &#39;exe&#39;;
            break;
        case 7784:
            $fileType = &#39;midi&#39;;
            break;
        case 8297:
            $fileType = &#39;rar&#39;;
            break;        
  case 8075:
            $fileType = &#39;zip&#39;;
            break;
        case 255216:
            $fileType = &#39;jpg&#39;;
            break;
        case 7173:
            $fileType = &#39;gif&#39;;
            break;
        case 6677:
            $fileType = &#39;bmp&#39;;
            break;
        case 13780:
            $fileType = &#39;png&#39;;
            break;
        default:
            $fileType = &#39;unknown: &#39;.$typeCode;
    }
 //Fix
 if ($strInfo[&#39;chars1&#39;]==&#39;-1&#39; AND $strInfo[&#39;chars2&#39;]==&#39;-40&#39; ) return &#39;jpg&#39;;
 if ($strInfo[&#39;chars1&#39;]==&#39;-119&#39; AND $strInfo[&#39;chars2&#39;]==&#39;80&#39; ) return &#39;png&#39;;
    return $fileType;
}
echo file_type(&#39;start.php&#39;);   // 6063 or 6033

In this way we can limit the types of uploaded files and also provide a safe process for the program

The above is the detailed content of Detailed explanation of method example of PHP judging whether file has been uploaded. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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