php文件上传之原理分析与上传类代码
php教程文件上传之原理分析与上传类代码
今天研究PHP注册POST/GET大变量的时候,看到了关于这块的一些东西,跟踪了半天,,先记录下来,免得以后再如此麻烦的跟踪
处理器注册:
[mod_php5.c, mod_php5模块初始化]
php_init_handler(server_rec *s, pool *p)
->[main/SAPI.c]sapi_startup(&apache_sapi_module)
->[main/SAPI.c]
sapi_globals_ctor(&sapi_globals)
->[main/php_content_types.c]php_setup_sapi_content_types(TSRMLS_C)
->[main/php_content_types.c
php_post_entries如下]sapi_register_post_entries(php_post_entries
TSRMLS_CC)
->[main/SAPI.c]sapi_register_post_entry(p
TSRMLS_CC)
如下面的代码,共注册了俩个处理器,分别处理post数据和文件上传。
注1:参看在PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究
[main/rfc1867.h]
#define
MULTIPART_CONTENT_TYPE
"multipart/form-data"
[main/php_content_types.h]
#define
DEFAULT_POST_CONTENT_TYPE
"application/x-www-form-urlencoded"
[main/SAPI.c]
struct
_sapi_post_entry
{
char *content_type;
uint
content_type_len;
void
(*post_reader)(TSRMLS_D);
void
(*post_handler)(char *content_type_dup, void *arg
TSRMLS_DC);
};
[main/php_content_types.c]
static
sapi_post_entry
php_post_entries[] = {
{
DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler
},
{
MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler
},
{
NULL, 0, NULL, NULL
}
};
<?php
#*********************************************************
#文件名称: yl_upload.class.php
#功能描述: 印像上传类
#程序制作:留印(adleyliu)
#联系qq :14339095
#联系邮箱:adleyliu@163.com
#最后更新: 2007-11-11
#注:转发时请保留此声明信息,这段声明不并会影响你的速度!
#如有修改请将修改后的文件以邮件形式发送给作者一份,谢谢!
#
#*********************************************************
/*
//使用说明:
//声明一个上传类
$yl_upload = new yl_upload_class();
//设置参数
global $yl_filedata,$yl_directroy,$file_urldirectroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
$yl_filedata = 'filedata';//表单名
$yl_directroy = '../upload_file';//上传主目录
$file_urldirectroy = 'upload_file';//文件url(在编辑器KindEditor中用到,请查看upload.php实例)
$yl_settingsnew = 'month_'.date('Ym');//上传主目录1按版块存入不同目录,2按月份存入不同目录,3按天存入不同目录
$yl_maxsize = 2097152; //这里以字节为单位(1024*2)*1024=2097152 = 2M
$yl_sizeformat = 'k'; //显示文件大小单位b,k,m
$yl_arrext = array('gif','jpg','jpeg','png','bmp');//允许上传文件类型
$yl_ext = 0; //0原文件类型上传,1统一为存为jpg
//上传
$yl_upload -> yl_uploadfile();
*/
class yl_upload_class
{
#*********************************************************
#创建目录函数
#*********************************************************
function createfolder($yl_path)
{
if (!file_exists($yl_path))
{
$this -> createfolder(dirname($yl_path));
@mkdir($yl_path, 0777);
}
return $this -> createfolder;
}
#*********************************************************
#获取文件名称,大小,类型,临时文件名
#*********************************************************
function yl_getfilename($yl_type)
{
global $yl_filedata,$yl_directroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
return $_FILES[$yl_filedata][$yl_type];
}
#*********************************************************
#获取文件大小
#*********************************************************
function yl_getfilesize()
{
global $yl_filedata,$yl_directroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
if($this -> yl_getfilename('size') == 0){
$this -> alert("请选择上传文件!");
exit;
}
if($this -> yl_getfilename('size') > $yl_maxsize){
$yl_maxsizek=$yl_maxsize/1024;
$this -> alert("上传文件超出限制范围$yl_maxsizek.K!");
exit;
}
switch (strtolower($yl_sizeformat)){
case 'b':
return $this -> yl_getfilename('size') . ' B';
break;
case 'k':
return ($this -> yl_getfilename('size')/1024) . ' K';
break;
case 'm':
return ($this -> yl_getfilename('size'))/(1024*1024) . ' M';
}
}
#*********************************************************
#获得文件扩展名
#*********************************************************
function yl_getfiletype()
{
global $yl_filedata,$yl_directroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
$yl_temp_arr = explode(".", $this -> yl_getfilename('name'));
$yl_file_ext = array_pop($yl_temp_arr);
$yl_file_ext = trim($yl_file_ext);
$yl_file_ext = strtolower($yl_file_ext);
//检查扩展名
if (in_array($yl_file_ext, $yl_arrext) === false) {
$this -> alert("上传文件类型被限制!");
exit;
}
return $yl_file_ext;
}
#*********************************************************
#上传
#*********************************************************
function yl_uploadfile()
{
global $yl_filedata,$yl_directroy,$file_urldirectroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
$yl_file_nameold = $this -> yl_getfilename('name');//原文件名
$yl_file_namenews = date('Ymd').'_'.md5(date('YmdHis'));//重命名
if($yl_ext == 0){
$yl_file_names = $yl_file_namenews.'.'.$this -> yl_getfiletype();//改名
}elseif ($yl_ext == 1){
$yl_file_names = $yl_file_namenews.'.jpg';//统一改名为jpg
}
$yl_tmp_name = $this -> yl_getfilename('tmp_name');//服务器上临时文件名
$yl_file_size = $this -> yl_getfilesize();//获取文件大小
$yl_file_type = $this -> yl_getfiletype();//获取文件类型
$yl_file_path = $yl_directroy.'/'.$yl_settingsnew;//建立一个目录
//检查目录是否存在,不存在则创建
if(@is_dir($yl_file_path) === false) {
$this -> createfolder(''.$yl_file_path.'');//创建目录
}
//检查是否已上传
if(@is_uploaded_file($yl_tmp_name) === false) {
$this -> alert("文件已上传!");
exit;
}
//检查目录写权限
if (@is_writable($yl_file_path) === false) {
$this -> alert("上传目录没有写权限!");
exit;
}
$yl_doupload = @copy($yl_tmp_name, ''.$yl_file_path.'/'.$yl_file_names.'');
if($yl_doUpload === false)
{
$this -> alert("上传失败!");
}else{
echo '上传成功';
echo '<br>';
echo '文件目录:'.$yl_file_path.'';
echo '<br>';
echo '原文件名:'.$yl_file_nameold.'';
echo '<br>';
echo '新文件名:'.$yl_file_names.'';
echo '<br>';
echo '文件大小:'.$yl_file_size.'';
echo '<br>';
echo '文件类型:'.$yl_file_type.'';
}
return;
}
#*********************************************************
#*删除文件
#*********************************************************
function delfile()
{
global $yl_filedata,$yl_directroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
$yl__dir = dirname(trim($yl_directroy));
if( $this->_isDel( $yl__dir ) )
{
return @unlink( $yl_directroy ) ? true : false;
}else{
return false;
}
}
#*********************************************************
#删除目录 目录下如果有文件不能删除
#*********************************************************
function deldir( )
{
global $yl_filedata,$yl_directroy,$yl_settingsnew;
global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
if( $this->_isdel($yl_directroy) && is_dir( $yl_directroy ) )
{
return @rmdir( $yl_directroy ) ? true : false;
}else{
return false;
}
}
#*********************************************************
#提示
#*********************************************************
function alert($yl_msg)
{
echo '<html>';
echo '<head>';
echo '<title>error</title>';
echo '<meta http-equiv="content-type" c>';
echo '</head>';
echo '<body>';
echo '<script type="text/网页特效">alert("'.$yl_msg.'");;</script>';
echo '</body>';
echo '</html>';
exit;
}
}
?>
PHP 支持同时上传多个文件并将它们的信息自动以数组的形式组织。要完成这项功能,需要在 HTML 表单中对文件上传域使用和多选框与复选框相同的数组式提交语法。
<title> 多个文件上传 </title>
<link rel= "stylesheet " type= "text/css教程 " href= "my.css ">
</head>
<body>
<FORM action= "upload_array.php教程 " method= "POST " name= "upload_multifiles " enctype= "multipart/form-data ">
请选择文件: <br>
<INPUT TYPE= "FILE " name= "userfile[] " size= "45 "> <br>
<INPUT TYPE= "FILE " name= "userfile[] " size= "45 "> <br>
<INPUT TYPE= "FILE " name= "userfile[] " size= "45 "> <br>
<INPUT TYPE= "FILE " name= "userfile[] " size= "45 "> <br>
<INPUT TYPE= "FILE " name= "userfile[] " size= "45 "> <br>
<INPUT TYPE= "SUBMIT " name= "submit " value= "开始上传 "></FORM>
当以上表单被提交后,数组 $_FILES['userfile'],$_FILES['userfile']['name'] 和 $_FILES['userfile']['size'] 将被初始化(在 PHP 4.1.0 以前版本是 $HTTP_POST_FILES)。如果 register_globals 的设置为 on,则和文件上传相关的全局变量也将被初始化。所有这些提交的信息都将被储存到以数字为索引的数组中。
例如,假设名为 /home/test/review.html 和 /home/test/xwp.out 的文件被提交,则 $_FILES['userfile']['name'][0] 的值将是 review.html,而 $_FILES['userfile']['name'][1] 的值将是 xwp.out。类似的,$_FILES['userfile']['size'][0] 将包含文件 review.html 的大小,依此类推。
此外也同时设置了 $_FILES['userfile']['name'][0],$_FILES['userfile']['tmp_name'][0],$_FILES['userfile']['size'][0] 以及 $_FILES['userfile']['type'][0]。
php文件
upload_array.php
--------------------------------
<?php
for($i=0;$i <count($userfile);$i++)
{
if($userfile_name[$i] <> " ")//判断上传的文件是否为空
{
copy($userfile[$i],$userfile_name[$i]);echo "文件名: ",$userfile_name[$i], " <br> ";
echo "文件大小: ",$userfile_size[$i], " <br> ";
echo "文件类型: ",$userfile_type[$i];
}
}
?>
他们利用了递归的方法来实例目录遍历,可以查找出无限级目录的文件与文件夹中的文件并显示,下面是实例代码
<?php教程
$dir = 'f:game';
function read_dir_all($dir) {
$ret = array('dirs'=>array(), 'files'=>array());
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file != '.' && $file !== '..') {
$cur_path = $dir . directory_separator . $file;
if(is_dir($cur_path)) {
$ret['dirs'][$cur_path] = read_dir_all($cur_path);
} else {
$ret['files'][] = $cur_path;
}
}
}
closedir($handle);
}
return $ret;
}
$p = read_dir_all($dir);
echo '<pre>';
var_dump($p);
echo '</pre>';
?>
php教程 soap 调用获取返回信息
trace soap message
获取 soap返回信息
<?php
$ws = "http://www.111cn.net/sd/2001/temperatureservice.wsdl";
$zipcode = "23590";
$client = new soapclient($ws,array('trace' => 1));
$temperature = $client->gettemp($zipcode);
echo htmlspecialchars($client->__getlastrequest());
?>
得到函数原型从wsdl
<?php
$ws = "http://www.www.111cn.net/sd/2001/temperatureservice.wsdl";
$client = new soapclient($ws);
var_dump($client->__getfunctions());
?>
<?php
$ws = "http://www.xmethods.net/sd/2001/temperatureservice.wsdl";
$zipcode = "12312";
$client = new soapclient($ws,array('trace' => 1));
$temperature = $client->gettemp($zipcode);
echo htmlspecialchars($client->__getlastresponse());
?>
由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php教程 以我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例。
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1><h3>demonstrates checkboxes</h3>
<form action ="handleformcheckbox.php"><ul>
<li><input type ="checkbox" name ="chkfries" value ="11.00">fries</li>
<li><input type ="checkbox" name ="chksoda" value ="12.85">soda</li>
<li><input type ="checkbox" name ="chkshake" value ="1.30">shake</li>
<li><input type ="checkbox" name ="chkketchup" value =".05">ketchup</li>
</ul>
<input type ="submit">
</form></body>
</html>
<!-- handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>here;
$total = 0;
if (!empty($chkfries)){
print ("you chose fries <br>");
$total = $total + $chkfries;
}if (!empty($chksoda)){
print ("you chose soda <br>");
$total = $total + $chksoda;
}if (!empty($chkshake)){
print ("you chose shake <br>");
$total = $total + $chkshake;
}if (!empty($chkketchup)){
print ("you chose ketchup <br>");
$total = $total + $chkketchup;
}print "the total cost is $$total";
?>
</body>
</html>
-->
实例
<html>
<head>
<title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server['php_self']);
if (!empty($food)) {
echo "the foods selected are:<br />";
foreach($food as $foodstuf)
{
echo "<strong>".htmlentities($foodstuf)."</strong><br />";
}
}
else
{
echo ("<form action="$self" ");
echo ('method="get">
<fieldset>
<label>italian <input type="checkbox" name="food[]" value="italian" />
</label>
<label>mexican <input type="checkbox" name="food[]" value="mexican" />
</label>
<label>chinese <input type="checkbox" name="food[]" value="chinese"
checked="checked" /></label>
</fieldset>
<input type="submit" value="go!" >');
}
?>
</body>
</html>
多选checkbox
<?php
$options = array('option 1', 'option 2', 'option 3');$valid = true;
if (is_array($_get['input'])) {
$valid = true;
foreach($_get['input'] as $input) {
if (!in_array($input, $options)) {
$valid = false;
}
}
if ($valid) {
//process input
}
}
?>
实例checkbox多值获取
<html>
<head>
<title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get["food"];//www.3ppt.com
if (!empty($food)){
echo "the foods selected are: <strong>";
foreach($food as $foodstuff){
echo '<br />'.htmlentities($foodstuff);
}
echo "</strong>.";
}
else {
echo ('
<form action="'. htmlentities($_server["php_self"]).'" method="get">
<fieldset>
<label>
italian
<input type="checkbox" name="food[]" value="italian" />
</label>
<label>
mexican
<input type="checkbox" name="food[]" value="mexican" />
</label>
<label>
chinese
<input type="checkbox" name="food[]" value="chinese" checked="checked" />
</label>
</fieldset>
<input type="submit" value="go!" />
</form> ');
}
?>
</body>
</html>
相关文章
- 下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
Jupyter Notebook读取csv文件出现的问题及解决
这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
- 有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
- 这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
php无刷新利用iframe实现页面无刷新上传文件(1/2)
利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25- 本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25
- 要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
- 公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
- 又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
- 本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24