十进制转二进制、八进制、十六进制 不足位数前面补零

 更新时间:2016年11月25日 16:26  点击:2589

 <?php教程
/**
*十进制转二进制、八进制、十六进制 不足位数前面补零*
*
* @param array $datalist 传入数据array(100,123,130)
* @param int $bin 转换的进制可以是:2,8,16
* @return array 返回数据 array() 返回没有数据转换的格式
* @copyright chengmo qq:8292669
*/
function decto_bin($datalist,$bin)
{
static $arr=array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f');
if(!is_array($datalist)) $datalist=array($datalist);
if($bin==10)return $datalist; //相同进制忽略
$bytelen=ceil(16/$bin); //获得如果是$bin进制,一个字节的长度
$aoutchar=array();
foreach ($datalist as $num)
{
$t="";
$num=intval($num);
if($num===0)continue;
while($num>0)
{
$t=$arr[$num%$bin].$t;
$num=floor($num/$bin);
}
$tlen=strlen($t);
if($tlen%$bytelen!=0)
{
$pad_len=$bytelen-$tlen%$bytelen;
$t=str_pad("",$pad_len,"0",str_pad_left).$t; //不足一个字节长度,自动前面补充0
}
$aoutchar[]=$t;
}
return $aoutchar;
}

本文章是利用了php一个插件实例邮php文件上传进度条的功能,方法比较简单,因为都有组件了,所以只要按照人家的意思照办就可以实例php大文件上传的功能了。

目前我知道的方法有两种,一种是使用php的创始人 rasmus lerdorf 写的apc扩展模块来实现(http://pecl.php.net/package/apc),另外一种方法是使用pecl扩展模块uploadprogress实现(http://pecl.php.net/package/uploadprogress) 我这里举两个分别实现的例子供参考,更灵活的应用根据自己需要来修改。

apc实现方法:

安装apc,参照官方文档安装,可以使用pecl模块安装方法快速简捷,这里不说明

配置php.ini,设置参数 apc.rfc1867=1 ,使apc支持上传进度条功能,在apc源码说明文档里面有说明

 

php文件上传进度条实现方法:安装apc,参照官方文档安装,可以使用pecl模块安装方法快速简捷,这里不说明 配置php.ini,设置参数 apc.rfc1867=1 ,使apc支持上传进度条功能,在apc源码说明文档里面有说明 代码范例:

if($_server['request_method']=='post'){//上传请求
$status=apc_fetch('upload_'.$_post['apc_upload_progress']);
$status['done']=1;
echojson_encode($status);//输出给用户端页面里的ajax调用,相关文档请自己寻找
exit;
}elseif(isset($_get['progress_key'])){//读取上传进度
$status=apc_fetch('upload_'.$_get['progress_key']);
echojson_encode($status);
exit;
}else{
//其他代码,比如上传表单等
}
uploadprogress 模块实现方法:使用pecl模块安装方法安装该模块的php文件上传进度条实现方法 php.ini里面设置 uploadprogress.file.filename_template = "/tmp/upd_%s.txt"

if($_server['request_method']=='post'){
if(is_uploaded_file($_files['upfile']['tmp_name'])){
$upload_dir='your_path/';
$ext=strrchr($_files['video']['name'],'.');
$sessid=$_post['upload_identifier'];
$tmpfile=$upload_dir.$sessid;
$sessfile=$upload_dir.$sessid.$ext;
if(move_uploaded_file($_files['upfile']['tmp_name'],$tmpfile)){
//上传成功
}else{
//上传失败
}else{
//上传错误

}elseif(!empty($_get['sessid'])){
header("expires:mon,26jul199705:00:00gmt");
header("last-modified:".gmdate("d,dmyh:i:s")."gmt");
header("cache-control:no-store,no-cache,must-revalidate");
header("cache-control:post-check=0,pre-check=0′,false);
header("pragma:no-cache");
header("content-type:text/html;charset=utf-8′);

$unique_id=$_get['sessid'];
$uploadvalues=uploadprogress_get_info($unique_id);

if(is_array($uploadvalues)){
echo  json_encode($uploadvalues);
}else{
//读取进度失败,另外处理逻辑
}

}else{
//显示上传表单
}


pecl扩展模块uploadprogress实现。


基于php的ajax技术的具体应用解析
php限制上传文件大小的具体解决办法
php批量上传图片的具体实现方式
php动态多文件上传的具体代码分享
php通用文件上传类的具体解析 我这里举两个分别实现的例子供参考,更灵活的应用根据自己需要来修改。

apc的php文件上传进度条实现方法:

安装apc,参照官方文档安装,可以使用pecl模块安装方法快速简捷

文章提供一款php检测文件类型(根据文件header信息)哦,他可以根据用户发布的文件头部信息来确实文件的类型。
 代码如下 复制代码
<?php
/*通过文件名,获得文件类型*
*@author chengmo*
*@copyright cnblog.com/chengmo 2010-10-17
*@version 0.1
*$filename="d:/1.png";echo cfiletypecheck::getfiletype($filename); 打印:png
*/
class cfiletypecheck
{
private static $_typelist=array();
private static $checkclass=null;
private function __construct($filename)
{
self::$_typelist=$this->gettypelist();
}
/**
*处理文件类型映射关系表*
*
* @param string $filename 文件类型
* @return string 文件类型,没有找到返回:other
*/
private function _getfiletype($filename)
{
$filetype="other";
if(!file_exists($filename)) throw new exception("no found file!");
$file = @fopen($filename,"rb");
if(!$file) throw new exception("file refuse!");
$bin = fread($file, 15); //只读15字节 各个不同文件类型,头信息不一样。
fclose($file);
$typelist=self::$_typelist;
foreach ($typelist as $v)
{
$blen=strlen(pack("h*",$v[0])); //得到文件头标记字节数
$tbin=substr($bin,0,intval($blen)); ///需要比较文件头长度
if(strtolower($v[0])==strtolower(array_shift(unpack("h*",$tbin))))
{
return $v[1];
}
}
return $filetype;
}
/**
*得到文件头与文件类型映射表*
*
* @return array array(array('key',value)...)
*/
public function gettypelist()
{
return array(array("ffd8ffe1","jpg"),
array("89504e47","png"),
array("47494638","gif"),
array("49492a00","tif"),
array("424d","bmp"),
array("41433130","dwg"),
array("38425053","ps教程d"),
array("7b5c727466","rtf"),
array("3c3f786d6c","xml"),
array("68746d6c3e","html"),
array("44656c69766572792d646174","eml"),
array("cfad12fec5fd746f","dbx"),
array("2142444e","pst"),
array("d0cf11e0","xls/doc"),
array("5374616e64617264204a","mdb"),
array("ff575043","wpd"),
array("252150532d41646f6265","eps/ps"),
array("255044462d312e","pdf"),
array("e3828596","pwl"),
array("504b0304","zip"),
array("52617221","rar"),
array("57415645","wav"),
array("41564920","avi"),
array("2e7261fd","ram"),
array("2e524d46","rm"),
array("000001ba","mpg"),
array("000001b3","mpg"),
array("6d6f6f76","mov"),
array("3026b2758e66cf11","asf"),
array("4d546864","mid"));
}
public static function getfiletype($filename)
{
if(!self::$checkclass) self::$checkclass=new self($filename);
$class=self::$checkclass;
return $class->_getfiletype($filename);
}
}

header("pragma:no-cachern");
  header("cache-control:no-cachern");
  header("expires:0rn");
  $rmurl = "http://www.111cn.net/demodata.txt";
 
 $infostring = file_get_contents($rmurl) or die("连接远程网址失败!");
 $infos = split(',',$infostring);
 $maxnum  =count($infos);
  $rmurl = trim($infos[rand(0,$maxnum-1)]);
 
  $zipbin = file_get_contents($rmurl);
 $fp = fopen(dirname(__file__).'/dedev56demo.zip','w');
 fwrite($fp,$zipbin);
 unset($zipbin);
 fclose($fp);

 

下面我收藏了三php删除文件哦,他可以利用unlink删除任何可删除的文件,本程序要删除文件,图片,压缩文件等,我们利用了几种方法来实例

三种php删除文件代码

 代码如下 复制代码

if($od=opendir($d))   //$d是目录名
{
        while(($file=readdir($od))!==false)  //www.111cn.net 读取目录内文件
        {
        unlink($file);  //$file是文件名
        }
}

去了解下这三个php函数:opendir()  readdir()  unlink()

文件删除代码二 系统调用法

 

 代码如下 复制代码
function del_dir($dir)
{
if(strtoupper(substr(php_os, 0, 3)) == 'win') {
       $str = "rmdir /s/q " . $dir;
} else {
       $str = "rm -rf " . $dir;
}
}

文件删除代码三

 代码如下 复制代码
$info = "a123"; //目录名
if(is_dir($info)){
if(rmdir($info)){
echo "目录{$inof}删除完毕www.111cn.net";
}else{
echo "目录无法删除!原因--可能是目录下还存在文件!";
}
}
if(is_file($info)){
if(unlink($info)){
echo "文件{$info}删除完毕...!";
}else{
echo "文件{$info}mb.111cn.net删除失败...!尝试修改文件权限删除...";
if(chmod($info,0777)){
unlink($info);
echo "文件{$info}权限修改后删除完毕...";
}else{
echo "文件{$info}无法通过web方式删除,可能是ftp权限对此文件有所设置...";
}
}
}

最近发现很多网站转载本站文件,注请加上本站连接,谢谢合作!

[!--infotagslink--]

相关文章