PHP small code
// Obtain an online file 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 (); // enable the output buffer set_time_limit ($ maxExe); // enable the maximum running time readfile ($ url ); // read a file and write it to the output buffer $ data = ob_get_contents (); ob_end_clean (); file_put_contents ($ file, $ data ); if ($ safe & is_executable ($ file) {// for security reasons, determine whether the file can be executed unlink ($ file); return false;} return $ file ;} getUrlImage (' http://www.test.com/3675.jpg ', 'Newname'); // Generate 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' => 'Lily ', 'age' => 15); mySetCookie ($ data, 'userinfo'); print_r ($ _ COOKIE); // bubble sort 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); // reverse the array element} // select the sort 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 ;}} // Insert the sorting function inserSort (& $ arr) {$ times = count ($ ar R); 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 ;}// calculates the relative paths of the two files. Function relative_dir ($ fileA, $ fileB) {// directory where A is located in 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 ++;} // accumulates parts of the same path if ($ aPath [$ I]! = $ BPath [$ I]) {$ path. = '.. /';} // different, the higher level will be returned} $ 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); // download an object as an attachment: $ file = 'E:/personal resume .doc '; $ file = iconv ('utf-8', 'gb2312', $ file); if (file_exists ($ file) {$ fname = basename ($ file ); $ fsize = filesize ($ file); header ("Content-type: applicati On/octet-stream "); // binary data header (" Content-Disposition: attachment; filename = {$ fname }"); // The Attachment form header ("Accept-ranges: bytes"); header ("Accept-length :". $ fsize); readfile ($ file);} else {exit ('flie not found! ');} Traverses a directory and its subdirectories: 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); // Obtain the file extension from the URL: function getExt ($ url) {$ arr = parse_url ($ url ); // Parse the URL into an array $ file = basename ($ arr ['path']); $ ext = explode ('. ', $ file); return end ($ ext);} // PHP verifies the email format 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);} // File upload script function addUpload () {document. getElementById (" upfiles "). innerHTML + ='
File:';} Function resetUpload () {document. getElementById ("upfiles"). innerHTML ='
File:';} Script $ files = 'Files'; // files is an array of elements in $ _ files, and the uploaded file information is classified as $ 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 ('upload directory creation failed! '); $ 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]; // specify the Directory, the file name is move_uploaded_file ($ _ FILES [$ files] ['tmp _ name'] [$ I], $ path ); // move to the specified directory if ($ _ FILES [$ files] ['error'] [$ I] = 0) {$ file [$ _ FILES [$ files] ['name'] [$ I] = $ pa Th; list ($ name, $ path) = each ($ file); // each (array) returns the current array consisting of key names and key values; list (variable 1, variable n [or array]) = an array of numeric indexes. assign the value to the variable. $ SQL = "INSERT INTO 'database '. 'Table' (name, path) VALUES ('$ name',' $ path') "; $ msg [] = $ value. 'File uploaded successfully';} else $ msg [] = $ value. 'File Upload failed! ';} The else $ msg [] = $ value.' file format is incorrect! ';} Return $ msg;} print_r (upFilse ($ files, $ upDir, $ fTypes); // calculate the day of the week: $ time = getdate (); // Obtain the time information in the current timestamp $ weekday = array ('Sunday', 'Monday', 'Tuesday', 'weday', 'thurs', 'Friday ', 'Saturday'); $ wday = $ time ['wday']; // The Day of the week. echo date ("today is: Y, m, d, H: i: s $ weekday [$ wday] "); // specifies the number of months and days for the next week: $ time = time (); // Current timestamp $ weekday = date ('w'); // The number of the day of the week switch ($ weekday) {case 0: $ nextMonday = $ time + 86400; break; // add one day to the day of the week. case 1: $ nextMond Ay = $ time + 7*86400; break; // Monday, plus seven days 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 ); // read the content of the specified number of rows of the file row by row: 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 );} // delete images not recorded in the database in the directory. 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.'
';}}