PHP 小代码
//获取网上的一个文件function getUrlImage($url, $file = '', $maxExe = 0, $safe = false){ $urlExt = explode('.', $url); $fileExt = array('txt','jpg','gif','png'); if(!in_array(end($urlExt), $fileExt, true)) return false; $file = ($file)? $file.$urlExt : basename($url); $file = rand(1,1000).$file; ob_start(); //开启输出缓冲 set_time_limit($maxExe); //开启最大运行时间 readfile($url);//读入一个文件并写入到输出缓冲 $data = ob_get_contents(); ob_end_clean(); file_put_contents($file,$data); if($safe && is_executable($file)){//为安全起见,判定一下文件是否可执行 unlink($file); return false; } return $file;}getUrlImage('http://www.test.com/3675.jpg','newName');//批量生成cookiefunction mySetCookie($data, $name){ if(empty($data) || empty($name))return; $args = func_get_args(); $time = empty($args[2])? time() + 3600 : time() + $args[2]; $path = empty($args[3])? '' : $args[3]; $domain = empty($args[4])? '' : $args[4]; $secure = empty($args[5])? '' : $args[5]; if(is_array($data)){ foreach($data as $key => $val){ $full = "{$name}[$key]"; setcookie($full, $val, $time, $path, $domain, $secure); } }else{ setcookie($name, $data, $time, $path, $domain, $secure); }}$data = array('name' => '李四', 'age' => 15);mySetCookie($data,'userInfo');print_r($_COOKIE);//冒泡排序function arrSort(&$arr, $asc = ''){ $times = count($arr) - 1; for($i = 0; $i < $times; $i++){ for($j = 0; $j < $times - $i; $j++){ if($arr[$j] > $arr[$j + 1]){ $temp = $arr[$j]; $arr[$j] = $arr[$j + 1]; $arr[$j + 1] = $temp; } } } if('' != $asc) $arr = array_reverse($arr, false);//反转数组元素}//选择排序function seleSort(&$arr){ $times = count($arr) - 1; $jMax = count($arr); for($i = 0; $i < $times; $i++){ $min = $arr[$i]; $minId = $i; for($j = $i + 1; $j < $jMax; $j++){ if($min > $arr[$j]){ $min = $arr[$j]; $minId = $j; } } $temp = $arr[$i]; $arr[$i] = $arr[$minId]; $arr[$minId] = $temp; }}//插入排序function inserSort(&$arr){ $times = count($arr); for($i = 1; $i < $times; $i++){ $insert = $arr[$i]; $insertId = $i - 1; while($insertId >= 0 && $insert < $arr[$insertId]){ $arr[$insertId + 1] = $arr[$insertId]; $insertId--; } $arr[$insertId + 1] = $insert; }}//计算两个文件的相对路径。function relative_dir($fileA, $fileB){//A相当于B,所在目录 $aPath = explode('/',dirname($fileA)); $bPath = explode('/',dirname($fileB)); $bLen = count($bPath); $j = 1; for($i = 1; $i < $bLen; $i++){ if(isset($bPath) && isset($aPath)){ if($aPath[$i] == $bPath[$i]){$j++;}//累计相同路径部分 if($aPath[$i] != $bPath[$i]){$path .= '../';}//不同的,则增加退回上级 } } $path .= implode('/',array_slice($aPath, $j)).'/'.basename($fileA); return $path;}$a = 'a/b/c/test/5/8/aaa.php';$b = 'a/b/c/check/1/2/3/4/bbb.php';echo relative_dir($a,$b);//以附件方式实现文件下载:$file = 'e:/个人简历.doc';$file = iconv('utf-8', 'gb2312',$file);if(file_exists($file)){ $fname = basename($file); $fsize = filesize($file); header("Content-type:application/octet-stream");//二进制数据 header("Content-Disposition:attachment;filename={$fname}");//附件形式 header("Accept-ranges:bytes"); header("Accept-length:".$fsize); readfile($file);}else{ exit('flie not found!');}遍历一个目录,及其子目录:function recurDir($pathName){ $result = array(); $temp = array(); if(!is_dir($pathName) || !is_readable($pathName)) return null; $allFiles = scandir($pathName); foreach($allFiles as $fileName){ if(in_array($fileName, array('.','..')))continue; $fullName = $pathName . '/' . $fileName; if(is_dir($fullName)){ $result[$fileName] = recurdir($fullName); }else{ $temp[] = $fileName; } } foreach($temp as $f){ $result[] = $f; } return $result;}$pathName = 'D:\AppServ\www\zbseoag\\';print_r(recurDir($pathName));//从URL中获取文件扩展名: function getExt($url){ $arr = parse_url($url);//把URL解析成数组 $file = basename($arr['path']); $ext = explode('.',$file); return end($ext);}//PHP验证email格式function checkEmail($email){ $pattern = "/([a-z0-9]*[-_/.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[/.][a-z]{2,3}([/.][a-z]{2})?/i"; return preg_match($pattern,$email); }//文件上传<script>function addUpload(){ document.getElementById("upfiles").innerHTML += '<li>文件:<input type="file" name="files[]" /></li>';}function resetUpload(){ document.getElementById("upfiles").innerHTML = '<li>文件:<input type="file" name="files[]" /></li>';}</script><form action="" method="post" enctype="multipart/form-data" ><ul id="upfiles"><li>文件:<input type="file" name="files[]" /></li></ul><input type="submit" value="提交" /><input type="button" value="增加上传框" onClick="addUpload()" /><input type="button" value="重设上传框" onClick="resetUpload()" /></form>$files = 'files';//files是$_FILES中的一个元素数组,并所上传文件信息进行了归类$upDir = './upImg/';$fTypes = 'jpg|gif|txt|chm';function upFilse($files, $upDir, $fTypes){ if(isset($_FILES[$files]['name'])){ if(!is_dir($upDir)) mkdir($upDir, 0777, true) or exit('上传目录创建失败!'); $ftypeArr = explode('|',$fTypes); foreach($_FILES[$files]['name'] as $i => $value){ $fType = strtolower(end(explode(".",$_FILES[$files]['name'][$i]))); if(in_array($fType, $ftypeArr)){ $path = $upDir.time().$_FILES[$files]['name'][$i];//指定目录,且包含有文件名 move_uploaded_file($_FILES[$files]['tmp_name'][$i], $path);//移到指定目录 if($_FILES[$files]['error'][$i] == 0){ $file[$_FILES[$files]['name'][$i]] = $path; list($name, $path) = each($file);//each(数组)返回当前由键名与键值所构成的数组;list(变量1, 变量n 【或数组】) = 数字索引的数组,将值赋给变量。 $sql = "INSERT INTO `database`.`table`(name, path) VALUES ('$name', '$path')"; $msg[] = $value.'文件上传成功'; }else $msg[] = $value.'文件上传失败!'; }else $msg[] = $value.'文件格式不正确!'; } return $msg; }}print_r(upFilse($files, $upDir, $fTypes));//求今天是星期几:$time = getdate();//获取当前时间戳中的时间信息$weekday = array('星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');$wday = $time['wday'];//当前是一个星期中的第几天echo date("今天是:Y年m月d日H:i:s $weekday[$wday]");//求下周一是几月几日:$time = time();//当前时间戳$weekday = date('w');//当天的数字星期switch($weekday){ case 0: $nextMonday = $time+86400;break;//星期天则加一天 case 1: $nextMonday = $time+7*86400;break;//星期一,则加七天 case 2: $nextMonday = $time+6*86400;break; case 3: $nextMonday = $time+5*86400;break; case 4: $nextMonday = $time+4*86400;break; case 5: $nextMonday = $time+3*86400;break; case 6: $nextMonday = $time+2*86400;break;}echo date('Y-m-d',$nextMonday);//逐行读取文件指定行数的内容:function getRowData($file, $row = 0, mark = false){ $fhandle = fopen($file,'rb'); $row = ($row == 0)? filesize($file) : $row; while($row >0 && !feof($fhandle)){ $data[] = (mark)? fgets($fhandle) : fgetss($fhandle); $row--; } fclose($fhandle);}//读取文件指定字符长度function getLetterData($file, $num = 0, mark = false){ $fhandle = fopen($file,'rb'); $row = ($num)? filesize($file) : $num; $data = fread($fhandle, $num); fclose($fhandle);}//删除目录中在数据库中没有记录的图片public function delImg($data, $dir = '.'){ $files = scandir($dir); $delFiles = array_diff($allFiles,$data); foreach($delFiles as $name){ $file = rtrim($dir,'/').'/'.$name; unlink($file); echo $file.'<br/>'; }}

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP中有四種主要錯誤類型:1.Notice:最輕微,不會中斷程序,如訪問未定義變量;2.Warning:比Notice嚴重,不會終止程序,如包含不存在文件;3.FatalError:最嚴重,會終止程序,如調用不存在函數;4.ParseError:語法錯誤,會阻止程序執行,如忘記添加結束標籤。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

在PHPOOP中,self::引用當前類,parent::引用父類,static::用於晚靜態綁定。 1.self::用於靜態方法和常量調用,但不支持晚靜態綁定。 2.parent::用於子類調用父類方法,無法訪問私有方法。 3.static::支持晚靜態綁定,適用於繼承和多態,但可能影響代碼可讀性。
