设为首页 收藏本站
查看: 844|回复: 0

[经验分享] some useful php function 2

[复制链接]

尚未签到

发表于 2017-3-28 13:11:50 | 显示全部楼层 |阅读模式
  断点续传

<?php
function sendfile($myFile){
$mm_type="application/octet-stream";
$fp = fopen($myFile,'rb');
$size = filesize($myFile);
ob_start();
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.$fname.'"');
header("Content-Transfer-Encoding: binaryn");  
if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $size)) {
$range = $match[1];
fseek($fp, $range);
header("HTTP/1.1 206 Partial Content");
//header("Date: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($myFile))." GMT");
header("Accept-Ranges: bytes");
$rangesize = ($size - $range) > 0 ? ($size - $range) : 0;
header("Content-Length:".$rangesize);
header("Content-Range: bytes ".$range.'-'.($size-1)."/".$size);
//header("Connection: close"." ");  
}else{
header("Content-Length: ".(string)($size));
header("Accept-Ranges: bytes");
$range = 0;
}  
fpassthru($fp);
ob_end_flush();  
}
?>
  裁减图片

<?
$FILENAME="test.jpg;
// 生成图片的宽度
$RESIZEWIDTH=400;
// 生成图片的高度
$RESIZEHEIGHT=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
  压缩文件

<?php
set_time_limit(0);
class phpzip{
var $file_count = 0 ;
var $datastr_len   = 0;
var $dirstr_len = 0;
var $filedata = ''; //该变量只被类外部程序访问
var $gzfilename;
var $fp;
var $dirstr='';
/*
返回文件的修改时间格式.
只为本类内部函数调用.
*/
function unix2dostime($unixtime = 0) {
$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
if ($timearray['year'] < 1980) {
$timearray['year']    = 1980;
$timearray['mon']     = 1;
$timearray['mday']    = 1;
$timearray['hours']   = 0;
$timearray['minutes'] = 0;
$timearray['seconds'] = 0;
}
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
}
/*
初始化文件,建立文件目录,
并返回文件的写入权限.
*/
function startfile($path = 'faisun.zip'){
$this->gzfilename=$path;
$mypathdir=array();
do{
$mypathdir[] = $path = dirname($path);
}while($path != '.');
@end($mypathdir);
do{
$path = @current($mypathdir);
@mkdir($path);
}while(@prev($mypathdir));
if($this->fp=@fopen($this->gzfilename,"w")){
return true;
}
return false;
}
/*
添加一个文件到 zip 压缩包中.
*/
function addfile($data, $name){
$name     = str_replace('\', '/', $name);
if(strrchr($name,'/')=='/') return $this->adddir($name);
$dtime    = dechex($this->unix2dostime());
$hexdtime = 'x' . $dtime[6] . $dtime[7]
. 'x' . $dtime[4] . $dtime[5]
. 'x' . $dtime[2] . $dtime[3]
. 'x' . $dtime[0] . $dtime[1];
eval('$hexdtime = "' . $hexdtime . '";');
$unc_len = strlen($data);
$crc     = crc32($data);
$zdata   = gzcompress($data);
$c_len   = strlen($zdata);
$zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
//新添文件内容格式化:
$datastr = "x50x4bx03x04";
$datastr .= "x14x00";            // ver needed to extract
$datastr .= "x00x00";            // gen purpose bit flag
$datastr .= "x08x00";            // compression method
$datastr .= $hexdtime;             // last mod time and date
$datastr .= pack('v', $crc);             // crc32
$datastr .= pack('v', $c_len);           // compressed filesize
$datastr .= pack('v', $unc_len);         // uncompressed filesize
$datastr .= pack('v', strlen($name));    // length of filename
$datastr .= pack('v', 0);                // extra field length
$datastr .= $name;
$datastr .= $zdata;
$datastr .= pack('v', $crc);                 // crc32
$datastr .= pack('v', $c_len);               // compressed filesize
$datastr .= pack('v', $unc_len);             // uncompressed filesize
fwrite($this->fp,$datastr); //写入新的文件内容
$my_datastr_len = strlen($datastr);
unset($datastr);
//新添文件目录信息
$dirstr = "x50x4bx01x02";
$dirstr .= "x00x00";                // version made by
$dirstr .= "x14x00";                // version needed to extract
$dirstr .= "x00x00";                // gen purpose bit flag
$dirstr .= "x08x00";                // compression method
$dirstr .= $hexdtime;                 // last mod time & date
$dirstr .= pack('v', $crc);           // crc32
$dirstr .= pack('v', $c_len);         // compressed filesize
$dirstr .= pack('v', $unc_len);       // uncompressed filesize
$dirstr .= pack('v', strlen($name) ); // length of filename
$dirstr .= pack('v', 0 );             // extra field length
$dirstr .= pack('v', 0 );             // file comment length
$dirstr .= pack('v', 0 );             // disk number start
$dirstr .= pack('v', 0 );             // internal file attributes
$dirstr .= pack('v', 32 );            // external file attributes - 'archive' bit set
$dirstr .= pack('v',$this->datastr_len ); // relative offset of local header
$dirstr .= $name;
$this->dirstr .= $dirstr; //目录信息
$this -> file_count ++;
$this -> dirstr_len += strlen($dirstr);
$this -> datastr_len += $my_datastr_len;
}
function adddir($name){
$name = str_replace("\", "/", $name);
$datastr = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00";
$datastr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
$datastr .= pack("v", 0 ).$name.pack("v", 0).pack("v", 0).pack("v", 0);
fwrite($this->fp,$datastr); //写入新的文件内容
$my_datastr_len = strlen($datastr);
unset($datastr);
$dirstr = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00";
$dirstr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
$dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );
$dirstr .= pack("v", 16 ).pack("v",$this->datastr_len).$name;
$this->dirstr .= $dirstr; //目录信息
$this -> file_count ++;
$this -> dirstr_len += strlen($dirstr);
$this -> datastr_len += $my_datastr_len;
}
function createfile(){
//压缩包结束信息,包括文件总数,目录信息读取指针位置等信息
$endstr = "x50x4bx05x06x00x00x00x00" .
pack('v', $this -> file_count) .
pack('v', $this -> file_count) .
pack('v', $this -> dirstr_len) .
pack('v', $this -> datastr_len) .
"x00x00";
fwrite($this->fp,$this->dirstr.$endstr);
fclose($this->fp);
}
}
?>

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-356523-1-1.html 上篇帖子: PHP多文件上传类 下篇帖子: PHP 图片处理类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表