Author Archives: Tim

php调整服务器时间

$today = date('Y-m-d-G');
$today = strftime("%Y-%m-%d-%H", strtotime("$today -5 hour"));

linux实时查看日志文件

tail -f /path/to/log_file

php获取图片信息

/*
 * @param string $file Filepath
 * @param string $query Needed information (0 = width, 1 = height, 2 = mime-type)
 * @return string Fileinfo
 */

function getImageinfo($file, $query) {
	if(!realpath($file)) {
		$file = $_SERVER["DOCUMENT_ROOT"].$file;
	}
	$image = getimagesize($file);
	return $image[$query];
}

shell脚本检测一个文件是否为空

#!/bin/bash
file="$1"
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
[ ! -f "$file" ] && { echo "Error: $0 file not found."; exit 2; }

if [ -s "$file" ]
then
  echo "$file has some data."
  # do something as file has data
else
  echo "$file is empty."
  # do something as file is empty
fi

php检测文件大小

/*
 * @param string $file Filepath
 * @param int $digits Digits to display
 * @return string|bool Size (KB, MB, GB, TB) or boolean
 */

function getFilesize($file, $digits = 2) {
	if(is_file($file)) {
		$filePath = $file;
		if(!realpath($filePath)) {
			$filePath = $_SERVER["DOCUMENT_ROOT"].$filePath;
		}
		$fileSize = filesize($filePath);
		$sizes = array("TB", "GB", "MB", "KB", "B");
		$total = count($sizes);
		while($total-- && $fileSize > 1024) {
			$fileSize /= 1024;
		}
		return round($fileSize, $digits)." ".$sizes[$total];
	}
	return false;
}

shell脚本检测一个文件是否存在

#!/bin/bash
FILE="$1"

if [ -f $FILE ];
then
  echo "File $FILE exists"
else
  echo "File $FILE does not exists"
fi

php验证邮箱地址是否有效

<?php
$email="test@example.com";
if(isValidEmail($email))
{
	echo "Email address is valid.";
}
else
{
	echo "Email address is not valid";
}

// Check-Function
function isValidEmail($email)
{
	//Perform a basic syntax-check
	//If this check fails, there's no need to continue
	if(!filter_var($email, FILTER_VALIDATE_EMAIL))
	{
		return false;
	}

	//extract host
	list($user, $host) = explode("@", $email);
	//check, if host is accessible
	if(!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
	{
		return false;
	}

	return true;
}
?>

shell脚本重启apache

#!/bin/sh
if ps auxc | grep httpd; then
  exit 0
else
  echo "HTTP service crash"
  /etc/init.d/httpd stop
  sleep 3
  /etc/init.d/httpd start
  echo "httpd restarted on server." | mail -s "httpd (`uname -n`) restarted @ `date`"
  mail@example.com
fi

shell脚本修改文件名为小写

for i in *.txt; do mv "$i" "`echo $i | tr [A-Z] [a-z]`"; done

php获取ip地址

<?php
$ip = $_SERVER["REMOTE_ADDR"];
echo "<br> Your IP address: " . $ip;
echo "<br> Your hostname: " . GetHostByName($ip);
?>