PHP file upload to determine whether file has chosen to upload files, _php tutorial

Source: Internet
Author: User
Tags php file upload

PHP file upload to determine whether file has chosen to upload files method,


This article describes the php file upload to determine whether file has chosen to upload files. Share to everyone for your reference. Here's how:

A qualified programmer in the implementation of data warehousing we will have some very strict filtering and data rules, such as our file upload in the previous paragraph to determine whether users choose to upload files at the same time in the background can also be judged whether there are uploaded files, this example is to do a more in-depth analysis.

This is shown in the following HTML code:
Copy the Code code as follows:

Our most commonly used in the front-end simple to judge
Copy the Code code as follows:
If we're going to be really safe, we need to get in the background.
Copy the Code code as follows: <?php
Determine if the pic file box has selected a file
if (!empty ($_files[' file ' [' Tmp_name '])) {
Echo ' selected file ';
}else{
echo ' Please select file ';
}
Ps:$_files back [' tmp_name '] must not forget to write, it means a temporary meaning
?>

Ann Case Analysis

JS Judgment is more general we just use the File=document.getelementbyid ("file"). Value, to determine whether the file has a value or not, so as long as the input of a number can be submitted directly, So we need to enter the user name restrictions such as uploading files
Such as
Copy CodeThe code is as follows: function Checkworkfile ()
{
var Obj=document.getelementbyid (' Fumain ');
if (obj.value== ")
{
Alert (' Please select the job book file to upload ');
return false;
}
var stuff=obj.value.match (/^ (. *) (\.) (. {1,8}) $/) [3];
if (stuff!= ' Doc ')
{
Alert (' file type is not correct, please select. doc file ');
return false;
}
return true;
}
For PHP processing We also only use if (!empty ($_files[' file ' [' Tmp_name '])) {To judge not to be empty, in fact this is also unreasonable
If we can handle this,
Copy CodeThe 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 ']== ') return ' PNG ';
return $fileType;
}
echo file_type (' start.php '); 6063 or 6033
This allows us to restrict the upload of file types and also gives the program a secure handling

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909343.html www.bkjia.com true http://www.bkjia.com/PHPjc/909343.html techarticle php File Upload to determine whether file has chosen to upload files, the example of this article tells the php file upload to determine whether the file has chosen the method of uploading files. Share to everyone 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.