溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php常用自定義函數(shù)

發(fā)布時(shí)間:2020-03-02 06:26:38 來源:網(wǎng)絡(luò) 閱讀:223 作者:飛揚(yáng)Tom 欄目:web開發(fā)

php常用自定義函數(shù)

<?php 
/**
 * 刪除指定路徑下文件
 * @param  [type] $path 文件路徑
 */
function delFile($path) {
    try {
        if(file_exists($path)) {
            unlink($path);
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
/**
 * 正則去掉字符串中的html標(biāo)簽
 * @param unknown $str
 * @return string
 */
function filterHtml($str) {
    $str = str_replace("&nbsp;","",strip_tags($str));
    $str = preg_replace('/((\s)*(\n)+(\s)*)/i','', $str);
    $str = trim($str);
    return $str;
}
/**
 * 將文件大小換算成合適的單位
 * @param int $size
 * @return $size
 */
function convertSize($size) {
    // Adapted from: http://www.php.net/manual/en/function.filesize.php  
    $mod = 1024;
    $units = explode(' ', 'B KB MB GB TB PB');
    for($i = 0; $size > $mod; $i ++) {
        $size /= $mod;
    }
    return round($size, 2).' '.$units[$i];
}
/**
 * 列出目錄下的文件名
 * @param  [type] $DirPath 目錄
 */
function listDirFiles($DirPath){
    if($dir = opendir($DirPath)){
        while(($file = readdir($dir))!== false){
            if(!is_dir($DirPath.$file)) { 
                echo "filename: $file<br />";
            } 
        } 
    }
}
/**
 * php強(qiáng)制下載文件
 * @param String $filename 文件名稱
 */
function download($filename){ 
    if ((isset($filename))&&(file_exists($filename))){ 
       header("Content-length: ".filesize($filename)); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename="' . $filename . '"'); 
       readfile("$filename"); 
    } else { 
       echo "Looks like file does not exist!"; 
    } 
}

/**
 * 防止網(wǎng)頁復(fù)制代碼
 */
echo "<script>
document.oncontextmenu=new Function("event.returnValue=false;");
document.onselectstart=new Function("event.returnValue=false;");
</script>
";
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI