Home > Article > Backend Development > Detailed explanation of method example of PHP judging whether file has been uploaded
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='multipart/form-data'> 文件上传:<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('请选择图片'); return false; } } </script>
If we want to make real securityWe need to enter judgment processing in the background
<?php //判断pic文件框是否已经选择文件 if(!empty($_FILES['file']['tmp_name'])){ echo'已选择文件'; }else{ echo'请选择文件'; } //PS:$_FILES后面的['tmp_name']一定不要忘写,它表示是一个临时的意思 ?>
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('fuMain'); if(obj.value=='') { alert('请选择要上传的作业书文件'); return false; } var stuff=obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; if(stuff!='doc') { alert('文件类型不正确,请选择.doc文件'); 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['chars1'].$strInfo['chars2']); $fileType = ''; switch ($typeCode) { case 7790: $fileType = 'exe'; break; case 7784: $fileType = 'midi'; break; case 8297: $fileType = 'rar'; break; case 8075: $fileType = 'zip'; break; case 255216: $fileType = 'jpg'; break; case 7173: $fileType = 'gif'; break; case 6677: $fileType = 'bmp'; break; case 13780: $fileType = 'png'; break; default: $fileType = 'unknown: '.$typeCode; } //Fix if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg'; if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png'; return $fileType; } echo file_type('start.php'); // 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!