PHP 小功能

1. 实现短连接的还原

function restoreUrl($shortUrl) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $shortUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    $data = curl_exec($curl);
    $curlInfo = curl_getinfo($curl);
    curl_close($curl);
    if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
        return $curlInfo['redirect_url'];
    }
    return '';
}

// 短连接还原
$shortUrl = 'https://m.tb.cn/h.ew5NAEA';    // 要还原的短网址
$orinalUrl = restoreUrl($shortUrl);
if($orinalUrl) {
    echo "短网址 {$shortUrl} 的还原结果:{$orinalUrl}";
} else {
    echo "短网址还原失败";
}

2. 随机一张图

$img_array = glob('image/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
if(count($img_array) == 0) die('没找到图片文件。请先上传一些图片到 '.dirname(__FILE__).'\image');
header('Content-Type: image/png');
$content = $img_array[array_rand($img_array)];
echo((file_get_contents($content)));

3. 随机一句话

// 存储数据的文件
$filename = 'data.dat'; // 文件中每句话占一行       
 
// 指定页面编码
header('Content-type: text/html; charset=utf-8');
 
if(!file_exists($filename)) {
    die($filename . ' 数据文件不存在');
}
 
$data = array();
 
// 打开文档
$fh = fopen($filename, 'r');

/**
* 读取数据方法一
*/
// 逐行读取并存入数组中
while (!feof($fh)) {
    $data[] = fgets($fh);
}
 
// 关闭文档
fclose($fh);
 
// 随机获取一行索引
$result = $data[array_rand($data)];
 
echo $result;
/**
* 读取数据方法二
*/
// 读取整个数据文件
$data = file_get_contents($filename);

// 按换行符分割成数组
$data = explode(PHP_EOL, $data);
 
// 随机获取一行索引
$result = $data[array_rand($data)];
 
// 去除多余的换行符(保险起见)
$result = str_replace(array("\r","\n","\r\n"), '', $result);
 
echo $result;

4. 并发下读写文件

<?php
 
/**
 * @link http://kodcloud.com/
 * @author warlee | e-mail:kodcloud@qq.com
 * @copyright warlee 2014.(Shanghai)Co.,Ltd
 * @license http://kodcloud.com/tools/license/license.txt
 */
 
 
/**
 * 安全读取文件,避免并发下读取数据为空
 * 
 * @param $file 要读取的文件路径
 * @param $timeout 读取超时时间 
 * @return 读取到的文件内容 | false - 读取失败
 */
function file_read_safe($file, $timeout = 5) {
    if (!$file || !file_exists($file)) return false;
    $fp = @fopen($file, 'r');
    if (!$fp) return false;
    $startTime = microtime(true);
    
    // 在指定时间内完成对文件的独占锁定
    do {
        $locked = flock($fp, LOCK_EX | LOCK_NB);
        if (!$locked) {
            usleep(mt_rand(1, 50) * 1000);     // 随机等待1~50ms再试
        }
    }
    while ((!$locked) && ((microtime(true) - $startTime) < $timeout));
    
    if ($locked && filesize($file) >= 0) {
        $result = @fread($fp, filesize($file));
        flock($fp, LOCK_UN);
        fclose($fp);
        if (filesize($file) == 0) {
            return '';
        }
        return $result;
    } else {
        flock($fp, LOCK_UN);
        fclose($fp);
        return false;
    }
}
 
/**
 * 安全写文件,避免并发下写入数据为空
 * 
 * @param $file 要写入的文件路径
 * @param $buffer 要写入的文件二进制流(文件内容)
 * @param $timeout 写入超时时间 
 * @return 写入的字符数 | false - 写入失败
 */
function file_write_safe($file, $buffer, $timeout = 5) {
    clearstatcache();
    if (strlen($file) == 0 || !$file) return false;
    
    // 文件不存在则创建
    if (!file_exists($file)) {
        @file_put_contents($file, '');
    }
    if(!is_writeable($file)) return false;    // 不可写
    
    // 在指定时间内完成对文件的独占锁定
    $fp = fopen($file, 'r+');
    $startTime = microtime(true);
    do {
        $locked = flock($fp, LOCK_EX); 
        if (!$locked) {
            usleep(mt_rand(1, 50) * 1000);   // 随机等待1~50ms再试
        }
    }
    while ((!$locked) && ((microtime(true) - $startTime) < $timeout));    
    
    if ($locked) {
        $tempFile = $file.'.temp';
        $result = file_put_contents($tempFile, $buffer, LOCK_EX);
        
        if (!$result || !file_exists($tempFile)) {
            flock($fp, LOCK_UN);
            fclose($fp);
            return false;
        }
        @unlink($tempFile);
        
        ftruncate($fp, 0);
        rewind($fp);
        $result = fwrite($fp, $buffer);
        flock($fp, LOCK_UN);
        fclose($fp);
        clearstatcache();
        return $result;
    } else {
        flock($fp, LOCK_UN);
        fclose($fp);
        return false;
    }
}

转载: https://mkblog.cn/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值