php 判断文件上传类型与过滤不安全数据

 更新时间:2016年11月25日 16:29  点击:1342

这函数 过滤不安全字符

function s_addslashes($string, $force = 0) {
 if(!get_magic_quotes_gpc()) {
  if(is_array($string)) {
   foreach($string as $key => $val) {
    $string[$key] = s_addslashes($val, $force);
   }
  } else {
   $string=str_replace("&#x","& # x",$string); //

过滤一些不安全字符
   $string = addslashes($string);
  }
 }
 return $string;
}

实例:
$_COOKIE = c_addslashes($_COOKIE);
$_POST   = c_addslashes($_POST);
$_GET   = c_addslashes($_GET);

在公共文件中加入

if($_FILES){ 
 foreach( $_FILES as $key => $_value )
 {
  $_FILES[$key]['type'] =$_value['type'];  
 }
 if(substr($_FILES[$key]['type'],0,6) !='image/')
 {
  exit;
 }
}

禁止上传除图片文件以外的文件,
提示:
不要获取文件扩展名来判断类型,这样是最不安全的,我们用$_FIlES['form']

['type']
这个可以读取文件内容来识别文件类型,但它能识别的有限,不过如果你用图片就足

够了解。

www.111cn.net 本站原创,转载注明

方法1 - 使用HTTP头

至于在MS Word中所述,您需要格式化的HTML / PHP页面使用Excel友好CSS和标头信息

添加到您的PHP脚本。
<?php
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=document_name.xls");

echo "<html>";
echo "<meta http-equiv="Content-Type" content="text/html;

charset=Windows-1252">";
echo "<body>";
echo "<b>testdata1</b> <u>testdata2</u> ";
echo "</body>";
echo "</html>";
?>

方法2 - 使用COM对象

请注意,在服务器运行下面必须有MS Excel中所述的代码安装。

我们使用一个文件保存到临时目录第一,作为MS Word的同样的做法。

//Create new COM object – excel.application
$xl = new COM("excel.application");

//Hide MS Excel application window
$xl->Visible = 0;

//Create new document
$xlBook = $xl->Workbooks->Add();

//Create Sheet 1
$xlBook->Worksheets(1)->Name = "Worksheet 1";
$xlBook->Worksheets(1)->Select;

//Set Width & Height
$xl->ActiveSheet->Range("A1:A1")->ColumnWidth = 10.0;
$xl->ActiveSheet->Range("B1:B1")->ColumnWidth = 13.0;

//Add text
$xl->ActiveSheet->Cells(1,1)->Value = "TEXT";
$xl->ActiveSheet->Cells(1,1)->Font->Bold = True;

//Save document
$filename = tempnam(sys_get_temp_dir(), "excel");
$xlBook->SaveAs($filename);

//Close and quit
unset( $xlBook);
$xl->ActiveWorkBook->Close();
$xl->Quit();
unset( $xl );

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=document_name.xls");

// Send file to browser
readfile($filename);
unlink($filename);

<?php教程
//目录名称合法性检测
function isen($str){
$ret="";
for($i=0;$i $p=ord(substr($str,$i,1));
if(($p<48 & $p!=45 & $p!=46) || ($p>57 & $p<65) || ($p>90 & $p<97 & $p!=95) || $p>122){
nerror("不符合规范!");
}
$ret.=chr($p);
}
return $ret;
}
?>

<?php

function image2ascii( $image )
{
    // return value
    $ret = '';

    // open the image
    $img = ImageCreateFromJpeg($image); 

    // get width and height
    $width = imagesx($img); 
    $height = imagesy($img); 

    // loop for height
    for($h=0;$h<$height;$h++)
    {
        // loop for height
        for($w=0;$w<=$width;$w++)
        {
            // add color
            $rgb = ImageColorAt($img, $w, $h); 
            $r = ($rgb >> 16) & 0xFF; 
            $g = ($rgb >> 8) & 0xFF; 
            $b = $rgb & 0xFF;
            // create a hex value from the rgb
            $hex = '#'.str_pad(dechex($r), 2, '0', STR_PAD_LEFT).str_pad(dechex($g), 2, '0', STR_PAD_LEFT).str_pad(dechex($b), 2, '0', STR_PAD_LEFT);

            // now add to the return string and we are done
            if($w == $width)
            { 
                $ret .= '<br>'; 
            }
            else
            { 
                $ret .= '<span style="color:'.$hex.';">#</span>'; 
            } 
        } 
    } 
    return $ret;
}
?>

Example Usage

<?php

// an image to convert
$image = 'test.jpg';

// do the conversion
$ascii = image2ascii( $image );

// and show the world
echo $ascii;
?>

 

添加附件
添加一个附件
添加一或多个附件很简单,添加附件,是通过调用addAttachment方法,这种方法可以多次调用添加多个attachemnts。

布尔addAttachment($文件的字符串,字符串[$ c_type ='应用程序/八位字节流'],串[$名称=],布尔[$ isfile =真],字符串[$编码='一个base64'])
变量:

$文件:要么变量包含一个文件的内容,或文件本身的路径
$ c_type:内容类型,这意味着,例如文件的MIME类型。
text / plain的,文字/ CSV格式,应用/ PDF格式
$名称:该文件的名称,您希望它出现在电子邮件,这应该是唯一的
$ isFile:是否变量$文件是对文件或文件的内容的路径
$编码:这通常应为默认离开,除非你知道你在做什么
附件可以是在一个变量,或在服务器上的文件中存储的文件系统。在这第一个例子中,我将建立一个小型文本文件名为'你好text.txt'改为'你好世界!也。

  1. <?
  2.         include('Mail.php');
  3.         include('Mail/mime.php');
  4.  
  5.         // Constructing the email
  6.         $sender = "Leigh <leigh@no_spam.net>";                              // Who your name and email address
  7.         $recipient = "Leigh <leigh@no_spam.net>";                           // The Recipients name and email address
  8.         $subject = "Test Email";                                            // Subject for the email
  9.         $text = 'This is a text message.';                                  // Text version of the email
  10.         $html = '<html><body><p>This is a html message</p></body></html>';  // HTML version of the email
  11.         $crlf = "n";
  12.         $headers = array(
  13.                         'From'          => $sender,
  14.                         'Return-Path'   => $sender,
  15.                         'Subject'       => $subject
  16.                         );
  17.  
  18.         // Creating the Mime message
  19.         $mime = new Mail_mime($crlf);
  20.  
  21.         // Setting the body of the email
  22.         $mime->setTXTBody($text);
  23.         $mime->setHTMLBody($html);
  24.  
  25.         // Add an attachment
  26.         $file = "Hello World!";                                      // Content of the file
  27.         $file_name = "Hello text.txt";                               // Name of the Attachment
  28.         $content_type = "text/plain";                                // Content type of the file
  29.         $mime->addAttachment ($file, $content_type, $file_name, 0);  // Add the attachment to the email
  30.  
  31.         $body = $mime->get();
  32.         $headers = $mime->headers($headers);
  33.  
  34.         // Sending the email
  35.         $mail =& Mail::factory('mail');
  36.         $mail->send($recipient, $headers, $body);
  37. ?>

    添加多个附件
    正如上一节,添加多个附件是rasy与调用addAttachment了。在这个例子中,我会发送一个带有两个文本附件的电子邮件。

    <?
            include('Mail.php');
            include('Mail/mime.php');
     
            // Constructing the email
            $sender = "Leigh <leigh@no_spam.net>";                              // Who your name and email address
            $recipient = "Leigh <leigh@no_spam.net>";                           // The Recipients name and email address
            $subject = "Test Email";                                            // Subject for the email
            $text = 'This is a text message.';                                  // Text version of the email
            $html = '<html><body><p>This is a html message</p></body></html>';  // HTML version of the email
            $crlf = "n";
            $headers = array(
                            'From'          => $sender,
                            'Return-Path'   => $sender,
                            'Subject'       => $subject
                            );
     
            // Creating the Mime message
            $mime = new Mail_mime($crlf);
     
            // Setting the body of the email
            $mime->setTXTBody($text);
            $mime->setHTMLBody($html);
     
            // Add an attachment
            $file = "Hello World!";                                      // Content of the file
            $file_name = "Hello text.txt";                               // Name of the Attachment
            $content_type = "text/plain";                                // Content type of the file
            $mime->addAttachment ($file, $content_type, $file_name, 0);  // Add the attachment to the email
     
            // Add a second attachment
            $file = "Hello World! Again :)";                             // Content of the file
            $file_name = "Hello text 2.txt";                             // Name of the Attachment
            $content_type = "text/plain";                                // Content type of the file
            $mime->addAttachment ($file, $content_type, $file_name, 0);  // Add the attachment to the email
     
            $body = $mime->get();
            $headers = $mime->headers($headers);
     
            // Sending the email
            $mail =& Mail::factory('mail');
            $mail->send($recipient, $headers, $body);
    ?>

[!--infotagslink--]

相关文章

  • Php文件上传类class.upload.php用法示例

    本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • jQuery实现简单的文件上传进度条效果

    本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • 借助FileReader实现将文件编码为Base64后通过AJAX上传

    这篇文章主要介绍了借助FileReader实现将文件编码为Base64后通过AJAX上传的方法,包括后端对文件数据解码并保存的PHP代码,需要的朋友可以参考下...2015-12-25
  • jQuery+ajax简单实现文件上传的方法

    这篇文章主要介绍了jQuery+ajax简单实现文件上传的方法,结合实例形式简单分析了jQuery基于ajax的post方法进行文件传输及asp.net后台处理技巧,需要的朋友可以参考下...2016-06-12
  • js实现上传文件添加和删除文件选择框

    这篇文章主要为大家详细介绍了js实现上传文件添加和删除文件选择框 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-25
  • 适用于初学者的简易PHP文件上传类

    本文实例讲述了PHP多文件上传类,分享给大家供大家参考。具体如下:<&#63;phpclass Test_Upload{ protected $_uploaded = array(); protected $_destination; protected $_max = 1024000; protected $_messages =...2015-10-30
  • js 实现文件上传样式详情

    这篇文章主要介绍了js 实现文件上传样式,下面文章举例说明js 是如何实现文件上传样式的,附有代码详细解说,需要的朋友可以参考一下,希望对你有所帮助...2021-10-21
  • PHP利用APC模块实现大文件上传进度条的方法

    php 大文件带进度的上传,一直是一个令php程序员很苦恼的问题。查询baidu 、Google ,大体做带进度的上传方式为:flash+php,socket,apc+php等,下面我介绍了apc +php+ajax制作的带进度的上传,并贴出源码,希望对大家有用。 Altern...2015-10-30
  • C#文件上传的简单实现

    这篇文章主要为大家详细介绍了C#文件上传的简单实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • php需登录的文件上传管理系统

    本文给大家介绍一个不错的需要登录的php 文件上传管理系统,功能简单有需要了解的同学可参考。 代码如下<&#63;php$admin_pw="admin";//管理密码$uploaddir="upload";//上传目录session_start();if($_GET['action']=="g...2015-10-30
  • asp.net html控件的File控件实现多文件上传实例分享

    asp.net中html控件的File控件实现多文件上传简单实例,开发工具vs2010使用c#语言,感兴趣的朋友可以了解下,必定是多文件上传值得学习,或许本文所提供的知识点对你有所帮助...2021-09-22
  • TypeScript前端上传文件到MinIO示例详解

    这篇文章主要为大家介绍了TypeScript前端上传文件到MinIO示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...2022-10-12
  • JQuery异步提交表单与文件上传功能示例

    这篇文章主要介绍了JQuery异步提交表单与文件上传功能,结合实例形式分析了jQuery表单提交及文件传输操作的相关实现技巧,需要的朋友可以参考下...2017-01-16
  • PHP文件上传主要代码讲解

    复制代码 代码如下:<?php if($_FILES['myfile']['name'] != '') { if($_FILES['myfile']['error'] > 0) { echo "错误状态:" . $_FILES['myfile']['error']; } else { move_uploaded_f...2013-10-04
  • 使用jQuery.form.js/springmvc框架实现文件上传功能

    这篇文章主要介绍了使用jQuery.form.jsspringmvc框架实现文件上传功能,非常具有参考借鉴价值,感兴趣的朋友一起学习吧...2016-05-14
  • c# 判断指定文件是否存在的简单实现

    这篇文章主要介绍了c# 判断指定文件是否存在的简单实现,需要的朋友可以参考下...2020-06-25
  • jquery插件uploadify实现带进度条的文件批量上传

    这篇文章主要介绍了jquery插件uploadify实现带进度条的文件批量上传,感兴趣的小伙伴们可以参考一下...2015-12-14
  • C#简单实现文件上传功能

    这篇文章主要介绍了C#简单实现文件上传功能,利用MVC+EF+LigerUI 实现的upload上传功能,感兴趣的小伙伴们可以参考一下...2020-06-25