// The keyword to be replaced $ GLOBALS ["patterns"] = array ( "/# BASE_URL #/" ); // The Replaced content corresponds to the above matching rules one by one $ GLOBALS ["replacements"] = array ( Http://api.map.baidu.com/lbsapi/cloud" // "Http: // 172.220.178/lbsapi /" // "Http://dev.baidu.com/wiki/static/map/cloud" ); /** * File operations * * Example: * FileUtil: copyDir ('B', 'd/e'); create a d/e folder in the copy folder and copy the content in the B folder. * FileUtil: copyFile ('B/1/2/3.exe', 'B/B/3.exe'); create a B/B folder for the copied file, copy the 3.exe file in the B/1/2 folder. * FileUtil: createDir ('a/1/2/3'); create a folder named a/1/2/3 * FileUtil: unlinkFile ('B/d/3.exe'); test deleting a file B/d/3.exe */ Class FileUtil { /** * Create a folder * * @ Param string $ aimUrl * @ Return viod */ Function createDir ($ aimUrl ){ $ AimUrl = str_replace ('', '/', $ aimUrl ); $ AimDir = ''; $ Arr = explode ('/', $ aimUrl ); Foreach ($ arr as $ str ){ $ AimDir. = $ str .'/'; If (! File_exists ($ aimDir )){ Mkdir ($ aimDir ); } } } /** * Delete an object * * @ Param string $ aimUrl * @ Return boolean */ Function unlinkFile ($ aimUrl ){ If (file_exists ($ aimUrl )){ Unlink ($ aimUrl ); Return true; } Else { Return false; } } /** * Copy a folder * * @ Param string $ oldDir * @ Param string $ aimDir * @ Param boolean $ overWrite this parameter controls whether to overWrite the original file * @ Return boolean */ Function copyDir ($ oldDir, $ aimDir, $ overWrite = false ){ $ AimDir = str_replace ('', '/', $ aimDir ); $ AimDir = substr ($ aimDir,-1) = '/'? $ AimDir: $ aimDir .'/'; $ OldDir = str_replace ('', '/', $ oldDir ); $ OldDir = substr ($ oldDir,-1) = '/'? $ OldDir: $ oldDir .'/'; If (! Is_dir ($ oldDir )){ Return false; } If (! File_exists ($ aimDir )){ FileUtil: createDir ($ aimDir ); } $ DirHandle = opendir ($ oldDir ); While (false! ==( $ File = readdir ($ dirHandle ))){ If ($ file = '.' | $ file = '..'){ Continue; } If (! Is_dir ($ oldDir. $ file )){ FileUtil: copyFile ($ oldDir. $ file, $ aimDir. $ file, $ overWrite ); } Else { FileUtil: copyDir ($ oldDir. $ file, $ aimDir. $ file, $ overWrite ); } } Return closedir ($ dirHandle ); } /** * Copy an object * * @ Param string $ fileUrl * @ Param string $ aimUrl * @ Param boolean $ overWrite this parameter controls whether to overWrite the original file * @ Return boolean */ Function copyFile ($ fileUrl, $ aimUrl, $ overWrite = false ){ If (! File_exists ($ fileUrl )){ Return false; } If (file_exists ($ aimUrl) & $ overWrite = false ){ Return false; } Elseif (file_exists ($ aimUrl) & $ overWrite = true ){ FileUtil: unlinkFile ($ aimUrl ); } $ AimDir = dirname ($ aimUrl ); FileUtil: createDir ($ aimDir ); Copy ($ fileUrl, $ aimUrl ); // Replace the variable $ ApiFilePointer = fopen ($ aimUrl, 'r '); $ ApiFileContent = fread ($ apiFilePointer, filesize ($ aimUrl )); // Only replace JavaScript, html, and css files If (preg_match ('/(. js |. html |. css |. htm) $/', $ aimUrl )){ $ ApiFileContent = preg_replace ($ GLOBALS ["patterns"], $ GLOBALS ["replacements"], $ apiFileContent ); } Fclose ($ apiFilePointer ); Echo $ aimUrl. "rn "; $ ApiFilePointer = fopen ($ aimUrl, 'W + '); Fwrite ($ apiFilePointer, $ apiFileContent ); Fclose ($ apiFilePointer ); // Replace the variable Return true; } } FileUtil: copyDir ("resource", "cloud", true ); ?> |