PHP file upload to determine if file has chosen to upload files

Source: Internet
Author: User
Tags file upload php file php file upload

The following HTML code like

The code is as follows Copy Code

<formaction= "?" Method= "POST" enctype= ' Multipart/form-data ' >
File Upload: <inputtype= "file" name= "file" id= "file"/>
<inputtype= "Submit" id= "send" value= "submitted"/>
</form>


Our most commonly used in the front of the simple to judge

The code is as follows Copy Code

<script>
Varsend=document.getelementbyid ("send");
Send.onclick=function () {
Varfile=document.getelementbyid ("File"). Value;
if (file.length<1) {
Alert (' Please select Picture ');
Returnfalse;
}
}
</script>

If we're going to do real security, we need to get in the background to judge.

The code is as follows Copy Code

<?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 behind the [' tmp_name '] must not forget to write, it means to be a temporary meaning
?>

An analysis of an example

JS Judgment is more general we just use the File=document.getelementbyid ("file"). value to determine whether the file has a value or is not empty, so as long as you enter a number can be directly submitted, So we need to go into the user name restrictions such as uploading files

Such as

The code is as follows Copy Code

Functioncheckworkfile ()
{
Varobj=document.getelementbyid (' Fumain ');
if (obj.value== ')
{
Alert (' Please select the work Book file to upload ');
Returnfalse;
}
Varstuff=obj.value.match (/^ (. *) (\.) (. {1,8}) $/) [3];
if (stuff!= ' Doc ')
{
Alert (' file type is incorrect, please select. doc file ');
Returnfalse;
}
Returntrue;
}

For PHP processing We also only use if (!empty ($_files[' file ' [' Tmp_name '])) {To judge is not empty, in fact, this is not reasonable

If we can handle this

The code is as follows Copy Code

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

So that we can limit the upload file type, but also to the program to do a security process OH

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.