PHP file upload method to determine whether file has been selected to upload a file,
The example in this article describes the method of PHP file upload to determine whether file has been selected to upload the file. Share it with everyone for your reference. The specific method is as follows:
A qualified programmer will have some very strict filtering and data rules when implementing data into the database. For example, when we upload files, we must determine whether the user chooses to upload the file in the front stage, and we can also determine whether there is an upload in the background. document, this article will provide a more in-depth analysis of this example.
As shown in the following html code:
Copy code The code is as follows:
We most commonly use simple judgment on the front end
Copy code The code is as follows:
<script><br>
var send=document.getElementById("send");<br>
send.onclick=function(){<br>
var file=document.getElementById("file").value;<br>
if(file.length<1){<br />
alert('Please select a picture');<br />
return false;<br />
}<br />
}<br />
</script>
If we want to achieve real security, we need to enter judgment processing in the background
Copy code The code is as follows:
//Determine whether the file has been selected in the pic file box
if(!empty($_FILES['file']['tmp_name'])){
echo 'File selected';
}else{
echo 'Please select a file';
}
//PS: Don’t forget to write ['tmp_name'] after $_FILES, it means a temporary meaning
?>
Analysis of Cases
js judgment is relatively general. We just use file=document.getElementById("file").value; to judge whether the file has a value or is not empty. In this way, you can submit it directly as long as you enter a number, so We need to enter restrictions such as username for uploading files
Such as
Copy code The code is as follows:
function CheckWorkFile()
{
var obj=document.getElementById('fuMain');
if(obj.value=='')
{
alert('Please select the homework file to upload');
return false;
}
var stuff=obj.value.match(/^(.*)(\.)(.{1,8})$/)[3];
if(stuff!='doc')
{
alert('The file type is incorrect, please select the .doc file');
return false;
}
return true;
}
For PHP processing, we only use if(!empty($_FILES['file']['tmp_name'])){ to determine if it is not empty. In fact, this is unreasonable
If we can handle it this way
Copy code The code is as follows:
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
这样我们可以限制上传文件类型的同时也给程序做了一个安全处理
希望本文所述对大家的PHP程序设计有所帮助。
http://www.bkjia.com/PHPjc/909343.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/909343.htmlTechArticlePHP文件上传判断file是否己选择上传文件的方法, 本文实例讲述了PHP文件上传判断file是否己选择上传文件的方法。分享给大家供大家参考。...