Common PHP Code Snippets for File Operations, Database Singleton, URL Handling, and Miscellaneous Utilities
This article presents a collection of practical PHP code snippets covering directory traversal, a MySQLi singleton class, fetching web content, extracting file extensions, converting strings, header redirects, regex cleaning, array filtering, date calculations, file locking, and date formatting.
This article provides a set of useful PHP code examples for everyday development tasks.
1. Traverse all files and directories
function fileShow($dir) {
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file !== '..' && $file !== '.') {
$f = $dir . '/' . $file;
if (is_file($f)) {
echo '|--' . $file . '<br>';
} else {
echo '--' . $file . '<br>';
fileShow($f);
}
}
}
}2. MySQLi singleton pattern
class db {
private $con;
private static $instance;
private function __construct($host, $username, $password, $database) {
$this->con = mysqli_connect($host, $username, $password);
if (!$this->con) die("连接失败!");
mysqli_select_db($database);
}
public static function getInstance($host, $username, $password, $database) {
if (self::$instance) return self::$instance;
self::$instance = new db($host, $username, $password, $database);
}
public function close() {
return mysqli_close($this->con);
}
private function __clone() {}
}3. Get content of a web page
$url = "http://www.phpres.com/index.html";
$str = file_get_contents($url);
// or using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);4. Extract file extension from a URL
function getExt1() {
$url_path = "http://www.sina.com.cn/abc/de/fg.php?id=1";
$temp = pathinfo($url_path, PATHINFO_EXTENSION);
$temp = explode("?", $temp);
echo $temp[0]; // php
}
function getExt2() {
$url_path = "http://www.sina.com.cn/abc/de/fg.php?id=1";
$temp = parse_url($url_path);
echo pathinfo($temp['path'], PATHINFO_EXTENSION);
}5. Get a file's extension
function get_ext1($file_name) { return strrchr($file_name, '.'); }
function get_ext2($file_name) { return substr($file_name, strrpos($file_name, '.')); }
function get_ext3($file_name) { $tmp = explode('.', $file_name); return array_pop($tmp); }
function get_ext4($file_name) { return pathinfo($file_name, PATHINFO_EXTENSION); }6. Convert snake_case to Camel Case
function conversion1() {
$str = "open_door";
return ucwords(str_replace('_', ' ', $str)); // Open Door
}7. Header redirects
header('location:index.php'); // redirect
header('HTTP/1.1 404 Not Found'); // send 404 status8. Remove all
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
