wuloves 发布的文章

压缩文件夹

因为打算使用php打包目录, 网上搜了搜现成的代码, 基本无用, 才写一下

<?php

function files($directory, &$fileList = [])
{
    $mydir = dir($directory);
    while ($file = $mydir->read()) {
        if ($file == "." || $file == "..") {
            continue;
        }
        if (is_dir("$directory/$file")) {
            files("$directory/$file", $fileList);
        } else {
            $fileList[] = $mydir->path . "/" . $file;
        }
    }
    $mydir->close();
    return $fileList;
}

function packageZip($dir, $savePath = null)
{
    if (!is_dir($dir)) {
        echo '<font style="color: red;">' . $dir . '不是一个目录</font>';
        return;
    }
    $savePath = empty($savePath) ? $dir . date("Y-m-d") . '.zip' : $savePath;
    $zip = new ZipArchive;
    is_file($savePath) ? unlink($savePath) : '';
    $res = $zip->open($savePath, ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach (files($dir) as $item) {
            $zip->addFile($item);
        }
        $zip->close();
    }
}

packageZip('wuloves');

1. 全局的代理设置

vi /etc/profile

添加下面内容

http_proxy = http://username:password@yourproxy:port/
ftp_proxy = http://username:password@yourproxy:port/
export http_proxy
export ftp_proxy

2. yum的代理设置

vi /etc/yum.conf

添加下面内容

proxy = http://username:password@yourproxy:port/

proxy=http://yourproxy:port
proxy=ftp://yourproxy:port
proxy_username=username
proxy_password=password

3. wget的代理设置

vi /etc/wgetrc

添加下面内容

# Proxy
http_proxy=http://username:password@proxy_ip:port/
ftp_proxy=http://username:password@proxy_ip:port/

  • 环境安装
yum install npm -y
npm install apidoc -g
apidoc -v
  • 在有文档的目录创建apidoc.json
{
  "name": "API名称",
  "version": "API主版本号,仅支持3位数字版本号,如:0.0.1",
  "description": "API描述信息",
  "title": "浏览器标题",
  "url": "可选,接口地址,如:http://wuloves.com/xxx",
  "sampleUrl": "可选,接口调用示例地址,如增加此项,则页面会增加可发送请求示例的操作项",
  "header": {
    "title": "可选,头部标题",
    "filename": "可选,头部文件路径,只支持.md文件"
  },
  "footer": {
    "title": "可选,底部描述",
    "filename": "可选,底部文件路径,只支持.md文件"
  },
  "template": {
    "withCompare": true, //是否允许版本之间进行比较
    "withGenerator": true//是否允许生成多个版本
  }
}
  • apidoc 使用说明
@api {get} /users/:user_id Request User Information
最主要的参数,”{get}”定义了HTTP请求是GET,API地址是”/users/:user_id”,文档中API的名称是”Request User Information”。
@apiVersion 0.1.0
API的版本号,默认显示在API名称的右方。该参数可用来在不同的版本之间做比较,后面会介绍。
@apiName GetUser
API名称,不影响文档。
@apiGroup User
API分组名,文档内容中和菜单栏中同一组的API会在一同显示,方便阅读。
@apiPermission admin
API的访问权限,文档中默认会API地址下面显示。没有权限要求的话,此项可以省略。
@apiDescription API to get the user information.
API的详细描述,默认显示在API名称的下方。
@apiExample Example usage:
API调用示例,该参数的下一行就是示例的内容,直到有空行结束。可以定义多个@apiExample,默认在文档中会以标签形式列出,标签名就是”Example usage:”。
@apiParam {Number} user_id The user’s unique ID.
API参数字段介绍,”{Number}”定义了字段类型,”user_id”是字段名称,后面则是字段描述。可以定义多个@apiParam字段。
@apiSuccess {String} name Name of the User.
API成功后返回的字段,如同@apiParam,”{String}”定义了字段类型,”name”是返回字段名称,后面则是字段描述。可以定义多个@apiSuccess字段。
@apiSuccessExample {json} Success-Response:
显示一个API成功返回后Response响应的示例,”{json}”代表响应体是JSON类型。该参数的下行就是响应体内容,直到有空行结束。可以定义多个@apiSuccessExample,默认在文档中会以标签形式列出,标签名就是”Success-Response:”。
@apiError UserNotFound User was not found.
API发生错误后的返回,”UserNotFound”是错误名称,后面则是错误描述。可以定义多个错误返回。
@apiErrorExample {json} Error-Response:
显示一个API错误返回后Response响应的示例,”{json}”代表响应体是JSON类型。该参数的下行就是响应体内容,直到有空行结束。可以定义多个@apiErrorExample,默认在文档中会以标签形式列出,标签名就是”Error-Response:”。
@apiSampleRequest http://localhost:5000/users/:user_id
文档提供的API Sample测试的地址。其实在”apidoc.json”中配过”sampleUrl”项后,此参数即可省去,除非这个API的测试URL比较特殊,需特别指定。

<?php
php header() 常用content-type
//定义编码
header( 'Content-Type:text/html;charset=utf-8 ');

//Atom
header('Content-type: application/atom+xml');

//CSS
header('Content-type: text/css');

//Javascript
header('Content-type: text/javascript');

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

//JSON
header('Content-type: application/json');

//PDF
header('Content-type: application/pdf');

//RSS
header('Content-Type: application/rss+xml; charset=ISO-8859-1');

//Text (Plain)
header('Content-type: text/plain');

//XML
header('Content-type: text/xml');

// ok
header('HTTP/1.1 200 OK');

//设置一个404头:
header('HTTP/1.1 404 Not Found');

// 请求频繁 429:
header('HTTP/1.1 429 Too Many Requests');

//设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');

//转到一个新地址
header('Location: http://www.example.org/');

//文件延迟转向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';

//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />

// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

//文档语言
header('Content-language: en');

//告诉浏览器最后一次修改时间
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

//告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');

//设置内容长度
header('Content-Length: 1234');

//设置为一个下载类型
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// 对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

//设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG***
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockw**e-flash'); //Flash动画

//显示登陆对话框
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';

// 下载xlsx文件
$filename = rtrim($_SERVER['DOCUMENT_ROOT'],'/').'/app/files/payment_status.csv';  
header('Content-Disposition: attachment; filename=payment_status.xlsx');  
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  
header('Content-Length: ' . filesize($filename));  
header('Content-Transfer-Encoding: binary');  
header('Cache-Control: must-revalidate');  
header('Pragma: public');  
readfile($filename);  

业务场景说明

  1. 比如扒人家的站点, 图片相对地址一个个的扒很不方便, 通过301重定向简单配置一下可以实现当图片没有下载到对应的相对位置时, 会自动重定向下载
  2. vue容器环境下, 每次发布新版本时, 保证缓存老代码的用户访问不会出现404js文件, 可以考虑一台机器用于存放这部分的js文件

保存到文件 auto_download_image.php

<?php
$host301 = "https://static.zennioptical.com" ;
if ($_SERVER["REQUEST_URI"]=='/' || substr( $_SERVER["REQUEST_URI"] ,strripos($_SERVER["REQUEST_URI"] , "/") )==str_replace(".","",substr( $_SERVER["REQUEST_URI"] ,strripos($_SERVER["REQUEST_URI"] , "/") ))  ){
    header("HTTP/1.1 404 Forbidden");
    echo $_SERVER["REQUEST_URI"] ;
    echo "<hr><a href='https://test.templete.com/b/all-glasses/_/category.html'>https://test.templete.com/b/all-glasses/_/category.html</a> ";
    echo "<hr><a href='https://www.zennioptical.com" . $_SERVER["REQUEST_URI"] . "' target='_blank'>https://www.zennioptical.com" . $_SERVER["REQUEST_URI"] . "</a> ";
    exit ;
}
if ( stripos($_SERVER["REQUEST_URI"] , "?") ){
    $file_url = $host301. substr( $_SERVER["REQUEST_URI"] , 0 ,stripos($_SERVER["REQUEST_URI"] , "?") )   ;
}else{
    $file_url = $host301. $_SERVER["REQUEST_URI"] ;
}
function SaveFileFromUrl($link) {
    echo $link ;
    $file_link = str_replace("//" ,"" , $link) ;
    $file_link = substr($file_link , stripos($file_link,"/")+1 ) ;
    $folder_link = substr($file_link , 0 ,strripos($file_link,"/")+1) ;
    $ch = curl_init ($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $img = curl_exec ($ch);
    curl_close ($ch);

//     $fp = fopen($image_name,'w'); // 下载图片到根目录

    if ( $img ){
        if (!is_dir($folder_link)) mkdir($folder_link , 0777 , true); // 如果不存在则创建
        $fp = fopen($file_link,'wb'); //下载图片到指定文件夹
        fwrite($fp, $img);
        fclose($fp);
//    header("HTTP/1.1 404 Forbidden");
        Header("HTTP/1.1 302 Moved Permanently");
        Header("Location: /" . $file_link );
    }else {
        echo "error" ;
    }


}
SaveFileFromUrl($file_url);

在静态文件目录下保存一个 .htaccess 文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ ../auto_download_image.php [L]
</IfModule>