PHP file Upload method for determining whether a file has been uploaded

Source: Internet
Author: User
Tags php file upload
Php file Upload determines whether a file has been uploaded ,. Php file Upload determines whether a file has been uploaded. This document describes how to upload a php file to determine whether a file has been uploaded. Share it with you for your reference. Php file Upload method to determine whether a file has been uploaded,

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.

Example: This article describes how to determine whether a file has been uploaded by uploading a php file. Share it with you for your reference ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.