PHP文件上传类

PHP文件上传类
PHP文件上传类

/**

* 验证码类

*

*/

/**

* 用例如下

完整写法

include_once(' cls.captcha.new.php');

$seccode = 'asdf';

$code = new Captcha();

$code->code = $seccode;

$code->width = 150;

$code->height = 60;

$code->background = 1;

$code->adulterate = 1;

$code->ttf = 1;

$code->angle = 1;

$code->color = 1;

$code->size = 1;

$code->shadow = 1;

$code->animator = 0;

$code->display();

$code->fontpath = ROOT_PATH.'/includes/captcha/fonts/'; $code->imagepath = ROOT_PATH.'/includes/captcha/';

简单用法

include_once(ROOT_PATH.'/includes/cls.captcha.new.php'); $seccode = 'asdf';

$code = new Captcha();

$code->code = $seccode;

$code->display();

*/

class Captcha

{

var $code; //a-z 范围内随机

var $width = 150; //宽度

var $height = 60; //高度

var $background = 1; //随机图片背景

var $adulterate = 1; //随机背景图形

var $ttf = 1; //随机TTF 字体

var $angle = 0; //随机倾斜度

var $color = 1; //随机颜色

var $size = 0; //随机大小

var $shadow = 1; //文字阴影

var $animator = 0; //GIF 动画

var $fontpath = ''; //TTF字库目录

var $imagepath = ''; //图片目录

var $fontcolor;

var $im;

function __construct($options = array())

{

$this->Captcha($options);

}

function Captcha($options = array())

{

$this->fontpath = ROOT_PATH . '/includes/captcha/fonts/';

$this->imagepath = ROOT_PATH . '/includes/captcha/';

isset($options['width']) && $this->width = $options['width'];

isset($options['height'])&& $this->height= $options['height'];

}

function display($code)

{

session_cache_limiter('nocache');

$this->code = strtoupper($code);

if (function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') && function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg')))

{

$this->image();

}

else

{

$this->bitmap();

}

}

function image()

{

$bgcontent = $this->background();

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

if ($this->animator == 1 && function_exists('imagegif'))

{

import('gifmerge.lib');

$trueframe = mt_rand(1, 9);

for ($i = 0; $i <= 9; $i++)

{

$this->im = imagecreatefromstring($bgcontent);

$x[$i] = $y[$i] = 0;

$this->adulterate && $this->adulterate();

if ($i == $trueframe)

{

$this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();

$d[$i] = mt_rand(250, 400);

}

else

{

$this->adulteratefont();

$d[$i] = mt_rand(5, 15);

}

ob_start();

imagegif($this->im);

imagedestroy($this->im);

$frame[$i] = ob_get_contents();

ob_end_clean();

}

$anim = new GifMerge($frame, 255, 255, 255, 0, $d, $x, $y, 'C_MEMORY');

header('Content-type: image/gif');

echo $anim->getAnimation();

}

else

{

$this->im = imagecreatefromstring($bgcontent);

$this->adulterate && $this->adulterate();

$this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();

if (function_exists('imagepng'))

{

header('Content-type: image/png');

imagepng($this->im);

}

else

{

header('Content-type: image/jpeg');

imagejpeg($this->im, '', 100);

}

imagedestroy($this->im);

}

}

function background() {

$this->im = imagecreatetruecolor($this->width, $this->height);

$backgroundcolor = imagecolorallocate($this->im, 255, 255, 255);

$backgrounds = $c = array();

if ($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') && function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY'))

{

if ($handle = @opendir($this->imagepath.'background/'))

{

while ($bgfile = @readdir($handle))

{

if (preg_match('/\.jpg$/i', $bgfile))

{

$backgrounds[] = $this->imagepath.'background/'.$bgfile;

}

}

@closedir($handle);

}

if ($backgrounds)

{

$imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);

$colorindex = imagecolorat($imwm, 0, 0);

$this->c = imagecolorsforindex($imwm, $colorindex);

$colorindex = imagecolorat($imwm, 1, 0);

imagesetpixel($imwm, 0, 0, $colorindex);

$c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];

imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);

imagedestroy($imwm);

}

}

if (!$this->background || !$backgrounds)

{

for ($i = 0; $i < 3; $i++)

{

$start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];

}

for ($i = 0; $i < $this->width; $i++)

{

$color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);

imageline($this->im, $i, 0, $i-$angle, $this->height, $color);

$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];

}

$c[0] -= 20;$c[1] -= 20;$c[2] -= 20;

}

ob_start();

if (function_exists('imagepng'))

{

imagepng($this->im);

}

else

{

imagejpeg($this->im, '', 100);

}

imagedestroy($this->im);

$bgcontent = ob_get_contents();

ob_end_clean();

$this->fontcolor = $c;

return $bgcontent;

}

function adulterate()

{

$linenums = $this->height / 10;

for ($i=0; $i <= $linenums; $i++)

{

$color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255),

mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

$x = mt_rand(0, $this->width);

$y = mt_rand(0, $this->height);

if (mt_rand(0, 1))

{

imagearc($this->im, $x, $y, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), $color);

}

else

{

imageline($this->im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->height, $this->width)), $color);

}

}

}

function adulteratefont()

{

$seccodeunits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

$x = $this->width / 4;

$y = $this->height / 10;

$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

for ($i = 0; $i <= 3; $i++)

{

$adulteratecode = $seccodeunits{mt_rand(0, 26)};

imagechar($this->im, 5, $x * $i + mt_rand(0, $x - 10), mt_rand($y, $this->height - 10 - $y), $adulteratecode, $text_color);

}

}

function ttffont()

{

$seccode = $this->code;

$charset = $GLOBALS['charset'];

$seccoderoot = $this->fontpath;

$dirs = opendir($seccoderoot);

$seccodettf = array();

while ($entry = readdir($dirs))

{

if ($entry != '.' && $entry != '..' && in_array(strtolower(file_ext($entry)), array('ttf', 'ttc')))

{

$seccodettf[] = $entry;

}

}

$seccodelength = 4;

$widthtotal = 0;

for ($i = 0; $i < $seccodelength; $i++)

{

$font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];

$font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;

$font[$i]['size'] = $this->width / 4;

$this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20);

$box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);

$font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);

$box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);

$font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);

$font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];

$font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8);

$font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width'];

$widthtotal += $font[$i]['width'];

}

$x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);

!$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

for ($i = 0; $i < $seccodelength; $i++)

{

if ($this->color)

{

$this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

$this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);

$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

}

elseif ($this->shadow)

{

$text_shadowcolor = imagecolorallocate($this->im, 255 -

$this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);

}

$y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);

$this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]);

imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font'], $seccode[$i]);

$x += $font[$i]['width'];

}

}

function giffont()

{

$seccode = $this->code;

$seccodedir = array();

if (function_exists('imagecreatefromgif'))

{

$seccoderoot = $this->imagepath.'gif/';

$dirs = opendir($seccoderoot);

while ($dir = readdir($dirs))

{

if ($dir != '.' && $dir != '..' && is_file($seccoderoot.$dir.'/9.gif'))

{

$seccodedir[] = $dir;

}

}

}

$widthtotal = 0;

for ($i = 0; $i <= 3; $i++)

{

$this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';

if (!empty($this->imcodefile) && is_file($this->imcodefile))

{

$font[$i]['file'] = $this->imcodefile;

$font[$i]['data'] = getimagesize($this->imcodefile);

$font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;

$font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;

$font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);

$widthtotal += $font[$i]['width'];

}

else

{

$font[$i]['file'] = '';

$font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);

$widthtotal += $font[$i]['width'];

}

}

$x = mt_rand(1, $this->width - $widthtotal);

for ($i = 0; $i <= 3; $i++)

{

$this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

if ($font[$i]['file'])

{

$this->imcode = imagecreatefromgif($font[$i]['file']);

if ($this->size)

{

$font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20);

$font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20);

}

$y = mt_rand(0, $this->height - $font[$i]['height']);

if ($this->shadow)

{

$this->imcodeshadow = $this->imcode;

imagecolorset($this->imcodeshadow, 0 , 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);

imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);

}

imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);

}

else

{

$y = mt_rand(0, $this->height - 20);

if ($this->shadow)

{

$text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);

imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);

}

$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);

imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);

}

$x += $font[$i]['width'];

}

}

function bitmap()

{

$numbers = array

(

'2' => array('fc','c0','60','30','18','0c','cc','cc','78','00'),

'3' => array('78','8c','0c','0c','38','0c','0c','8c','78','00'),

'4' => array('00','3e','0c','fe','4c','6c','2c','3c','1c','1c'),

'5' => array('78','8c','0c','0c','4c','78','60','60','7c','00'),

'7' => array('30','30','38','18','18','18','1c','8c','fc','00'),

'A' => array('00','EE','6c','7c','6c','6c','28','38','f8','00'),

'C' => array('00','38','64','c0','c0','c0','c4','64','3c','00'),

'E' => array('00','fe','62','62','68','78','6a','62','fe','00'),

'F' => array('00','f8','60','60','68','78','6a','62','fe','00'),

'H' => array('00','e7','66','66','66','7e','66','66','e7','00'),

'K' => array('00','f3','66','66','7c','78','6c','66','f7','00'),

'M' => array('00','f7','63','6b','6b','77','77','77','e3','00'),

'N' => array('00','ec','4c','54','54','54','64','64','ee','00'),

'P' => array('00','f8','60','60','7c','66','66','66','fc','00'),

'R' => array('00','f3','66','6c','7c','66','66','66','fc','00'),

'T' => array('00','78','30','30','30','30','b4','b4','fc','00'),

'V' => array('00','1c','1c','36','36','36','63','63','f7','00'),

'W' => array('00','36','36','36','77','7f','6b','63','f7','00'),

'X' => array('00','f7','66','3c','18','18','3c','66','ef','00'),

'Y' => array('00','7e','18','18','18','3c','24','66','ef','00'),

);

foreach ($numbers as $i => $number)

{

for ($j = 0; $j < 6; $j++)

{

$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);

$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0,

mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);

mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);

}

}

$bitmap = array();

for ($i = 0; $i < 20; $i++)

{

for ($j = 0; $j <= 3; $j++)

{

$bytes = $numbers[$this->code[$j]][$i];

$a = mt_rand(0, 14);

array_push($bitmap, $bytes);

}

}

for ($i = 0; $i < 8; $i++)

{

$a = substr('012345', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);

array_unshift($bitmap, $a);

array_push($bitmap, $a);

}

$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.

'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode( '', $bitmap));

header('Content-Type: image/bmp');

echo $image;

}

}

/**

* 生成随机串

*

* @param int $len

* @return string

function generate_code($len = 4)

{

$chars = '23457acefhkmprtvwxy';

for ($i = 0, $count = strlen($chars); $i < $count; $i++) {

$arr[$i] = $chars[$i];

}

mt_srand((double) microtime() * 1000000);

shuffle($arr);

$code = substr(implode('', $arr), 5, $len);

return $code;

}

?>

php文件上传(详细方法)

必须用post。 * 2.表单上传需要使用类型为file的input * 3.enctype="multipart/form-data"只有文件上传时才用这个,用来指定表单编码的数据方式。让服务器知道我们要传递文件。 *并带有一些常规的信息。加在form里面和method一样。*三、php处理上传的数据 *$_POST提取post方法提交的的表单数据,如果是文件上传的数据则使用$_FILES接收文件,而$_POST接收 *非文件数据。所以当要上传文件时用两种方法接收数据。点击上传后只要将临时文件移动到目标文件夹中就成功了。 *array(size=4) 'shopname'=>string'得到'(length=6) 'shoppri'=>string'得到'(length=6) 'shopnum'=>string'得到'(length=6) 'sub'=>string'添加商品'(length=12) array(size=1) 'pic'=> array(size=5) 'name'=>string'11112907_124211588000_2[1].jpg' (length=30)

文件上传漏洞及解析漏洞总结

文件上传漏洞及解析漏洞总结 文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务器端命令的能力。这种攻击方式是最为直接和有效的,“文件上传”本身没有问题,有问题的是文件上传后,服务器怎么处理、解释文件。如果服务器的处理逻辑做的不够安全,则会导致严重的后果。 文件上传后导致的常见安全问题一般有: 1)上传文件是Web脚本语言,服务器的Web容器解释并执行了用户上传的脚本,导致代码执行。 2)上传文件是Flash的策略文件crossdomain.xml,黑客用以控制Flash在该域下的行为(其他通过类似方式控制策略文件的情况类似); 3)上传文件是病毒、木马文件,黑客用以诱骗用户或者管理员下载执行。 4)上传文件是钓鱼图片或为包含了脚本的图片,在某些版本的浏览器中会被作为脚本执行,被用于钓鱼和欺诈。 除此之外,还有一些不常见的利用方法,比如将上传文件作为一个入口,溢出服务器的后台处理程序,如图片解析模块;或者上传一个合法的文本文件,其内容包含了PHP脚本,再通过"本地文件包含漏洞(Local File Include)"执行此脚本;等等。 要完成这个攻击,要满足以下几个条件: 首先,上传的文件能够被Web容器解释执行。所以文件上传后所在的目录要是Web容器所覆盖到的路径。 其次,用户能够从Web上访问这个文件。如果文件上传了,但用户无法通过Web访问,或者无法得到Web容器解释这个脚本,那么也不能称之为漏洞。 最后,用户上传的文件若被安全检查、格式化、图片压缩等功能改变了内容,则也可能导致攻击不成功。 一、从FCKEditor文件上传漏洞谈起 FCKEditor是一款非常流行的富文本编辑器,为了方便用户,它带有一个文件上传功能,但是这个功能却出过多次漏洞。 FCKEditor针对ASP/PHP/JSP等环境都有对应的版本,以PHP为例,其文件上传功能在:

php+html5实现无刷新上传、大文件分片上传、断点续传的方法

php+html5实现无刷新上传、大文件分片上传、断点续传的方法理清思路: 引入了两个概念:块(block)和片(chunk)。每个块由一到多个片组成,而一个资源则由一到多个块组成。 块是服务端的永久数据存储单位,片则只在分片上传过程中作为临时存储的单位。服务端会以约一个月为单位周期性的清除上传后未被合并为块的数据片。实现过程: 将文件分割,分片上传,然后合并。 前端代码: Document

kindEditor完整认识 PHP上调用并上传图片说明

引用: https://www.360docs.net/doc/8012259131.html,/yanghbmail/blog/item/6fc7beec9531e33462d09f2a.htm l kindEditor完整认识 PHP上调用并上传图片说明。 2011年02月03日星期四 20:26 最近又重新捣鼓了下kindeditor,之前写的一篇文章 https://www.360docs.net/doc/8012259131.html,/yanghbmail/blog/item/c681be015755160b1d9583e7.htm l感觉有点不太全面细致,所以今天再重新写下。 此文所述KE版本为当前2011年2月3日 20:10:18最新版KindEditor 3.5.2 先看下目录结构 我使用的是PHP 所以黄色区域的文件夹是可以删除的。 重命名为:kindeditor 看下我的WWW目录:

这里的关键文件就是图中的两个其它我划掉的是我自己的文件夹 -----------------dome.php---------------------- KindEditor PHP //此处的引入文件位置路径为相对于该PHP文件的位置此为demo.php //此处的引入文件位置路径为相对于该PHP文件的位置此为demo.php ?? ?? ??

php实现上传图片保存到数据库的方法

这篇文章主要介绍了php实现上传图片保存到数据库的方法,可通过将图片保存在数据库实现多台服务器共享文件的功能,非常具有实用价值,需要的朋友可以参考下php实现上传图片保存到数据库的方法。分享给大家供大家参考。具体分析如下: php 上传图片,一般都使用move_uploaded_file方法保存在服务器上。但如果一个网站有多台服务器,就需要把图片发布到所有的服务器上才能正常使用(使用图片服务器的除外)如果把图片数据保存到数据库中,多台服务器间可以实现文件共享,节省空间。 首先图片文件是二进制数据,所以需要把二进制数据保存在mysql数据库。 mysql数据库提供了BLOB类型用于存储大量数据,BLOB是一个二进制对象,能容纳不同大小的数据。 BLOB类型有以下四种,除存储的最大信息量不同外,其他都是一样的。可根据需要使用不同的类型。 TinyBlob 最大255B Blob 最大65K MediumBlob 最大16M LongBlob 最大4G 数据表photo,用于保存图片数据,结构如下: 代码如下: CREATE TABLE `photo` ( `id` int(10) unsigned NOT NULL auto_increment, `type` varchar(100) NOT NULL, `binarydata` mediumblob NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; upload_image_todb.php: 代码如下: <?php // 连接数据库 ","") or die(mysql_error()); @mysql_select_db('demo',$conn) or die(mysql_error()); // 判断action $action = isset($_REQUEST['action'])? $_REQUEST['action'] : ''; // 上传图片 if($action=='add'){ $image = mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name'])); $type = $_FILES['photo']['type']; $sqlstr = "insert into photo(type,binarydata) values('".$type."','".$image."')"; @mysql_query($sqlstr) or die(mysql_error()); header('location:upload_image_todb.php'); exit(); // 显示图片 }elseif($action=='show'){ $id = isset($_GET['id'])? intval($_GET['id']) : 0;

php处理ftp上传文件

用php实现文件上传二法 -------------------------------------------------------------------------------- PHP(Hypertext Preprocessor)是一种HTML 内嵌式的语言(类似IIS 上的ASP)。而PHP 独特的语法混合了C、Java、Perl 以及PHP 式的新语法。它可以比CGI 或者Perl&nb sp;更快速的执行动态网页。除此之外,用PHP 写出来的Web 后端CGI 程序,可以很轻易的移植到不同的系统平台上。 我们在做网站时,需要访问者的参于才能将网站建设得更加引人注目,这就要求我们从访问者那里得到文章、图片等。因此,文件上传成为网页中必不可少的功能。现在我就利用现在流行的编程语言PHP,用两种方法来说明其功能的实现。 一、利用php的文件函数来实现上传 这段代码分为两个文件,一个为upload.html,一个是upload.php。 上传文件选择:upload.html 代码如下: -------------------------------------------------------------------------------- 上载文件表单

请选择文件:

php多文件(图片)上传(精华版)

PHP多文件(图片)上传 总共三个文件conn.php index.php upload.php conn.php 文件 -------------------------------------------------------------------------------------------------------------- index.php 文件 -------------------------------------------------------------------------------------------------------------- 上传文件

upload.php 文件

PHP文件上传类

code = $seccode; $code->width = 150; $code->height = 60; $code->background = 1; $code->adulterate = 1; $code->ttf = 1; $code->angle = 1; $code->color = 1; $code->size = 1; $code->shadow = 1; $code->animator = 0; $code->display(); $code->fontpath = ROOT_PATH.'/includes/captcha/fonts/'; $code->imagepath = ROOT_PATH.'/includes/captcha/'; 简单用法 include_once(ROOT_PATH.'/includes/cls.captcha.new.php'); $seccode = 'asdf'; $code = new Captcha(); $code->code = $seccode; $code->display(); */ class Captcha { var $code; //a-z 范围内随机 var $width = 150; //宽度 var $height = 60; //高度 var $background = 1; //随机图片背景 var $adulterate = 1; //随机背景图形

php上传图片精典源码

//上传文件类型列表 $uptypes=array('image/jpg', 'image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image /x-png'); $max_file_size=5000000; //上传文件大小限制, 单位BYTE $destination_folder="../image/userhead/"; //上传文件路径 $watermark=1; //是否附加水印(1为加水印,其他为不加水印); $watertype=1; //水印类型(1为文字,2为图片) $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); $waterstring="newphp.site.cz"; //水印字符串 $waterimg="xplore.gif"; //水印图片 $imgpreview=1; //是否生成预览图(1为生成,其他为不生成); $imgpreviewsize=1/2; //缩略图比例 ?> M4U BLOG - https://www.360docs.net/doc/8012259131.html,

上传文件:





允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf

返回

解决Apache2的PHP上传文件大小的限制

post_max_size 属于 PHP_INI_PERDIR 属性,只能在 php.ini, .htaccess 或httpd.conf 中设置,如果 php.ini 或者 httpd.conf 都无权限的话(虚拟主机通常如此),那么只能通过 .htaccess 来设置(虚拟主机通常支持)。参考设置如下: .htaccess里写入 post_max_size 12m upload_max_filesize 12m max_execution_time 120 max_input_time 240 memory_limit 使用Apache时,使用PHP上传文件函数上传文件时,其容许的文件大小受系统配置的影响,一般需要修改php.ini: ?执行时间限制的延长或取消: max_execution_time 默认是30秒.改为 max_execution_time = 0 (0表示没有限制)或更大点的数字; 另一种方法是可以在php程序中加入 set_time_limit(); 来设定页面最久执行时间. set_time_limit(0);//0表示没有限制 ?查找 post_max_size = 2M (默认为2M)改为想要的大小,如: post_max_size = 100M 通常我们只做了这一步,以为就可以解决问题了。其实还需要看下面一步。 ?upload_max_filesize 表示所上传的文件的最大值。 查找upload_max_filesize,默认为8M改为 upload_max_filesize = 100M 需要注意的是,把post_max_size 大于 upload_max_filesize 的设置为好。 最后,设置结束后,需要重新启动apache。 具体可查看 PHP手册中的〔php.ini 核心配置选项说明〕 upload_max_filesize 所上传的文件的最大大小。 post_max_size 设定 POST 数据所允许的最大大小。 memory_limit 设定了一个脚本所能够申请到的最大内存字节数。

PHP与AJAX之间解决跨域上传文件与session丢失问题

PHP与AJAX之间解决跨域上传文件与session丢失问题 Author:Roc Yang 1.跨域问题 1.1 方法一: 使用jsonp(非官方)跨域数据交互协议,但是,这里存在一个局限性,就是不能上传文件(这里不详细说明,下面主要讲解方法二) 1.2 方法二: 直接使用json数据格式。 1.2.1 简单的允许所有域名对跨域到本站(不安全,不建议),只需在php文件中设置一下header: header('Access-Control-Allow-Origin: *');// *是表示允许所有域名进行跨域 1.2.2 只允许指定域名进行跨域,那么,程序就相对复杂一点了,但是比较安全和灵活: $domainAccess = array(//设置语序跨域的域名 'https://www.360docs.net/doc/8012259131.html,', 'https://www.360docs.net/doc/8012259131.html,', 'https://www.360docs.net/doc/8012259131.html,' ); $HTTP_REFERER = explode('/', $_SERVER["HTTP_REFERER"]);//获取发起请求的源站url,并分割成数组,$HTTP_REFERER[0]=>’http(s)’,$HTTP_REFERER[1]=>’’,$HTTP_REFERER[2]=>域名(如https://www.360docs.net/doc/8012259131.html,),$HTTP_REFERER[3…]=>uri if(in_array($HTTP_REFERER[2], $domainAccess)){//匹配域名 $serverHost = $HTTP_REFERER[0].'//'.$HTTP_REFERER[2]; header("Access-Control-Allow-Origin: ".$serverHost); } 2.session丢失问题 2.1 首先实现session共享 要实现session共享,有很多方法,可以使用关系型数据库共享(会增大数据库负载压力);可以使用redis、memcache等非关系型数据库共享(负载能力强,推荐),这些共享操作相对简单,只要在php.ini中找到session部分和php程序中配置好redis链接,php.ini配置如下(redis为例): session.save_handler = redis//类型 session.save_path="tcp://https://www.360docs.net/doc/8012259131.html,:123456?auth=adcbd"//存储或共享路径 session.cookie_domain ="https://www.360docs.net/doc/8012259131.html,"//域名(https://www.360docs.net/doc/8012259131.html,下的所有子域名) 2.2 ajax程序中添加如下代码(红色部分): var msg = new FormData($( "#form_chat_image" )[0]);//获取含有文件的form表单数据,form表单必须声明为:enctype="multipart/form-data" $.ajax({ type: "post", url: CHAT_SITE_URL+'/index.php?act=web_chat&op=send_pic', xhrFields: { withCredentials: true }, crossDomain: true,

php 修改上传文件大小

php 修改上传文件大小 有些朋友要通过自己的网站后台,包括论坛,来上传一些文件,php一般为2m,或8m(以下我们按默认为2m),接下来就是来讲怎么修改上传文件大小的。 1.首先修改执行上传文件限制 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完. 但在php中,默认的该页最久执行时间为 30 秒.就是说超过30秒,该脚本就停止执行. 这就导致出现无法打开网页的情况.这时我们可以修改 max_execution_time 在php.ini里查找 max_execution_time 默认是30秒.改为 max_execution_time = 0 0表示没有限制 另一种方法是可以在php程序中加入 set_time_limit(); 来设定页面最久执行时间. set_time_limit(0);//0表示没有限制 2.其次修改数据包的最大限制 修改 post_max_size 设定 POST 数据所允许的最大大小。此设定也影响到文件上传。 php默认的post_max_size 为2M.如果 POST 数据尺寸大于 post_max_size $_POST 和 $_FILES superglobals 便会为空. 查找 post_max_size .改为 post_max_size = 150M

3. 最好修改上传文件的最大限制 很多人都会改了第二步.但上传文件时最大仍然为 2M. 为什么呢.我们还要改一个参数upload_max_filesize 表示所上传的文件的最大大小。 查找upload_max_filesize,默认为8M改为 upload_max_filesize = 100M 另外要说明的是,post_max_size 大于 upload_max_filesize 为佳 相关参数解释: file_uploads = on ;是否允许通过HTTP上传文件的开关。默认为ON即是开 upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就会用系统默认的临时文件夹。 upload_max_filesize = 2m ;望文生意,即允许上传文件大小的最大值。默认为2M post_max_size = 8m ;指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。默认为8M max_execution_time = 30 ;每个PHP页面运行的最大时间值(秒),默认30秒 max_input_time = 60 ;每个PHP页面接收数据所需的最大时间,默认60秒 memory_limit = 128m ;每个PHP页面所吃掉的最大内存,默认128M。如果觉得小了,可以设置大点。128够用。 max_execution_time = 600 max_input_time = 600 upload_max_filesize = 32m post_max_size = 32m

文件上传漏洞(绕过方法)

文件上传校验姿势 ?客户端javascript校验(一般只校验后缀名) ?服务端校验 1)文件头content-type字段校验(image/gif) 2)文件内容头校验(GIF89a) 3)后缀名黑名单校验 4)后缀名白名单校验 5)自定义正则校验 ?WAF设备校验(根据不同的WAF产品而定) 文件上传绕过校验方法 客户端绕过(抓包改包) 方法:可以利用burp抓包改包,先上传一个gif类型的木马,然后通过 burp将其改为asp/php/jsp后缀名即可。 服务器端绕过 1)文件类型绕过 方法:通过抓包,将content-type字段改为image/gif。 2)文件头绕过 方法:在木马内容基础上再加了一些文件信息, 如GIF89a 3)文件后缀名绕过 即黑名单校验,一般有个专门的 blacklist 文件,里面会包含常见 的危险脚本文件。 方法: (1)找黑名单扩展名的漏网之鱼,如下jsp:jspx、jspf;asp:asa、 cer、aspx;php:php、php3、php4;exe:exee (2)可能存在大小写绕过漏洞,例如:aSp、pHp。 4)配合操作系统文件命令规则 (1)上传不符合windows文件命名规则的文件名: test.asp. test.asp(空格) test.php:1.jpg test.php::$DATA shell.php::$DATA…… (2)linux下后缀名大小写 5)配合其他规则 0x00截断:基于一个组合逻辑漏洞造成的,通常存在于构造上传文件路径的时候。test.php(0x00).jpg、test.php%00.jpg。 例如: 路径/upload/1.php(0x00),文件名1.jpg,结合/upload/1.php(0x00)/1.jpg 伪代码演示:

php上传图片

首先来看下上传部分的表单代码: CODE:

文件: < input type="submit" value="上传" name="B1">
这里有几个要注意的地方,首先看这句
,这里我们采用POST方法,个别浏览器还支持PUT方法,当然这需要对脚本进行修改,我并不建议这么做。表单中必须设置enctype="multipart/form-data,这样,服务器就知道上传文件带有常规表单信息,记住,这个是必须设置的。此外还需要一个隐藏域来限制上传文件的最大长度:,这里name必须设置成MAX_FILE_SIZE,其值就是上传文件的最大长度,单位是B,这里我限制成2M。再看这句:,type="file" 说明了文件类型,这样一个基本的上传文件接口就完成了,接下来讲讲如何用PHP来处理上传的文件,此外你的php.ini 中设置的上传文件最大长度可能会影响到你的实际上传,请根据实际情况修改,另PHP的上传是先传到临时目录,在移至指定目录的,了;临时目录的可根据

thinkphp实现带验证码的文件上传功能实例

本文实例讲述了thinkphp实现带验证码的文件上传功能。分享给大家供大家参考。具体实现方法如下: thinkphp上传文件非常的简单我们只要调用一个文件上传类uploadfile就可以快速的实现上传功能了,下面我来给大家整理了一个上传文件时需要验证功能的例子,希望文章对大家会带来帮助。 在模板中我们只需要调用就可以了 代码如下: <html> <head> <title>验证码</title> </head> <body> <formaction='__url__/login'method='post'enctype=multipart/form-data> 文件上传:<inputtype='file'name='imgage'><br/> 验证码:<inputtype='text'name='verify'> <imgsrc='__url__/verify'onclick='change()'id='verify'/><br/> <inputtype='submit'value='提交'> </form> </body> </html> php处理文件 代码如下: <?php classindexactionextendsaction{ /*验证码*/ publicfunctionverify(){ $type=isset($_get['type'])?$_get['type']:'gif';//如果不设置验证码的图片格式,默认为gif格式 import(@.org.image);//导入图片出来类 image::buildimageverify(4,1,$type);//设置验证码有几位,是数字,还是字母 } /*文件上传*/ publicfunctionupload(){ if(!empty($file)) import('@.org.uploadfile');//导入文件上传类 $file=newuploadfile();//实例化uploadfile类 //我们可以设置文件上传的一些属性 $file->maxsize=1000000;//设置上传图片大小 $file->allowextes=expload(',','jpg,jpeg,png,gif');//设置文件上传格式 $file->savepath='/tpl/default/public/uploads/';//设置图片存放位置 $file->thumb='true';//是否设置为缩略图 $file->thumbprefix='s_';//设置缩略图的前缀

PHP+MYSQL可以上传图像的超简单留言簿

PHP+MYSQL可以上传图像的超简单留言簿 https://www.360docs.net/doc/8012259131.html,/blog/static/5358671220100342857 819/ 第一步:新建一个数据库,名字为“guo“,再新建一个数据表,名字为"login",表中的字段分别为id,user,pw,imgpath。 第二步:新建一个可以连接数据的"con.php",里面写入: 第三步:写一个regist.php,可以注册,但是用户名不能重复。

if($_SERVER['REQUEST_METHOD']=='POST'){ $name=$_POST['user']; $file=$_FILES['file']; $rows=checkname($name); if($rows>0){ echo ""; } else{ if (!is_uploaded_file($file['tmp_name'])) //是否存在文件 { echo "图片不存在!"; exit; } if($file["size"]>$max_file_size){ echo "文件太大"; } if(!in_array($file['type'],$uptypes)){ echo "文件类型不符"; exit; } $filename=$file['tmp_name']; $image_size=getimagesize($filename); if(!file_exists($path)){ mkdir($path); } $name=$file["name"]; $path1=$path.$name; if(file_exists($path1)){ echo "同名文件已经存在了"; exit;} if(!move_uploaded_file($filename,$path1)){ echo "移动文件出错"; exit;} $sql="insert into login(id,user,pw,imgpath) values('0','$_POST[user]','$_POST[pw]','$path1')"; mysql_query($sql); echo ""; } }

相关主题
相关文档
最新文档