This article mainly introduces how to determine whether a file has been uploaded by uploading PHP files. In addition to determining whether an uploaded file exists, it also analyzes the file security in depth, it is a very practical skill.
This article mainly introduces how to determine whether a file has been uploaded by uploading PHP files. In addition to determining whether an uploaded file exists, it also analyzes the file security in depth, it is a very practical skill.
This example describes how to determine whether a file has been uploaded by uploading a PHP file. Share it with you for your reference. The specific method is as follows:
When a qualified programmer implements data warehouse receiving, we will have some very strict filtering and data rules, for example, when uploading a file, you need to determine whether the user chooses to upload the file and whether there is an uploaded file in the background. This article provides an in-depth analysis on this.
The following html code is used:
The Code is as follows:
The most common method is to make a simple judgment on the front end.
The Code is as follows:
Script
Var send = document. getElementById ("send ");
Send. onclick = function (){
Var file = document. getElementById ("file"). value;
If (file. length <1 ){
Alert ('select the image ');
Return false;
}
}
Script
To ensure real security, we need to go to the background for judgment and handling.
The Code is as follows:
<? Php
// Determine whether a file has been selected in the pic file box
If (! Empty ($ _ FILES ['file'] ['tmp _ name']) {
Echo 'selected files ';
} Else {
Echo 'select A file ';
}
// PS: ['tmp _ name'] After $ _ FILES must not be forgotten. It indicates a temporary meaning.
?>
Security Case Analysis
Js judgment is more general. We only use file = document. getElementById ("file "). value; to determine whether the file has a value or is not empty, so that you can directly submit the file by entering a number, so we need to enter the user name limit such as the file to be uploaded
For example
The Code is as follows:
Function CheckWorkFile ()
{
Var obj = document. getElementById ('fumain ');
If (obj. value = '')
{
Alert ('select the job book file to upload ');
Return false;
}
Var stuff = obj. value. match (/^ (. *) (\.) (. {1, 8}) $/) [3];
If (stuff! = 'Doc ')
{
Alert('specifies that the file is not deleted. Select the. Doc file ');
Return false;
}
Return true;
}
For php processing, we only use if (! Empty ($ _ FILES ['file'] ['tmp _ name']) {to judge whether it is null. In fact, this is unreasonable.
We can handle it like this.
The Code is as follows:
Function file_type ($ filename)
{
$ File = fopen ($ filename, "rb ");
$ Bin = fread ($ file, 2); // read-only 2 bytes
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 restrict the file type to be uploaded and make a security process for the program.
I hope this article will help you with PHP programming.