php打包zip文件,如果包含中文名,将文件名转化为GBK编码即可。

<?php
 
$zipfile = 'zipByPhp.zip';
 
$zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
if($zip->open($zipfile, ZIPARCHIVE::CREATE)!==TRUE){
    exit('create zip file error');
}
$files = array('queryimg.php', 'multicurl.php');
foreach($files as $file){
    $addfilename = iconv('UTF-8', 'GBK//IGNORE', $file);
    $zip->addFile($file, $addfilename);
}
$zip->close();

php curl上传文件

$field = array("upimg"=>"@/tmp/phpzip.zip");//文件路径,前面要加@,表明是文件上传.key与后台处理文件对应,使用$_FILES['upimg']获取
$curl = curl_init("http://localhost/a.php");
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$field);//这里的$field必须是数组结构,不要自作聪明使用 http_build_query,否则不认文件了
curl_exec($curl);

阅读全文

在做项目的时候,遇到一个需要处理xml文件的任务。把合作方传来的文件,加工下给引擎录入。

但是发现simpleXML没办法直接很方便的添加CDATA格式的数据,这样就会有很多问题。可能导致导出的xml格式错误。

找到了一个方法,分享给大家:

<?php
/**
* to show <title lang="en"><![CDATA[Site Title]]></title>   instead of <title lang="en">Site Title</title>
*
*/
class SimpleXMLExtended extends SimpleXMLElement
  {
  public function addCData($cdata_text)
    {
    $node = dom_import_simplexml($this); 
    $no   = $node->ownerDocument; 
    $node->appendChild($no->createCDATASection($cdata_text)); 
    } 
  }
$xmlFile    = 'config.xml';
// instead of $xml = new SimpleXMLElement('<sites/>');
$xml = new SimpleXMLExtended('<sites/>');
$site = $xml->addChild('site');
// instead of $site->addChild('site', 'Site Title');
$site->title = NULL; // VERY IMPORTANT! We need a node where to append
$site->title->addCData('Site Title');
$site->title->addAttribute('lang', 'en');
$xml->asXML($xmlFile);
?>

阅读全文

shell下,可以通过 “ls -l /usr/share/vim/vim70/colors/.”查看本机都安装了哪些主题,比如本人开发机,有如下主题:

blue.vim

darkblue.vim

default.vim

delek.vim

desert.vim

elflord.vim

evening.vim

koehler.vim

morning.vim

murphy.vim

pablo.vim

peachpuff.vim

ron.vim

shine.vim

slate.vim

torte.vim

zellner.vim

基本上是挨个试了一次,发现peachpuff是对php支持最好的,效果比较让人满意。

编辑~/.vimrc,添加一行

:colorscheme peachpuff

每次vim打开就是这个主题了。用起来比较舒服,配图一张:

vim-colorscheme-peachpuff-php


阅读全文

今天同事越到一个奇怪的问题,在下载头中指定了文件名,但是在ie6下,下载时却无法按给定的文件名给出保存,保存文件的名字为站点名称,在查阅一些资料后,确认为是gzip的问题,ie6不支持gzip,所以出现这个问题,但是在想关闭gzip的时候,发现关闭nginx的gzip压缩后,还是会有压缩头输出,后来才发现是ThinkPHP3.1版本新带的功能,自带“页面压缩输出支持”。

使用一个配置变量可以手动关闭:

‘OUTPUT_ENCODE’=>false


阅读全文

php header IE6文件下载失败问题

使用php header实现文件下载时,在IE6下总是提示不能下载,谷歌浏览器正常, 因此应该是IE的问题,IE下有个bug,微软官网有说明。错误消息:“Internet Explorer cannot download a file”(Internet Explorer 不能下载文件),有个办法可以很好的解决,看代码好了:

if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
	header("Cache-Control: no-cache");
	header("Pragma: no-cache");
}   
header('Cache-Control: public, must-revalidate, max-age=0');
header('Accept-Ranges: bytes');
header("Content-Type: application/force-download");  
header("Content-Type: application/download");  
header("Content-Type: application/octet-stream");  
if(!empty($content_type)){
	header("Content-Type: ".$content_type);  
}   
header("Content-Transfer-Encoding: binary");  
header('Content-Disposition:attachment;filename="'.rawurlencode($filename).'"');  

阅读全文

作者的图片

DigDeeply

Technology Stack: PHP/Openresty/GoLang, and so on…

Web Development Engineer

Beijing China