PHP implements traversing the directory and deleting the specified content in the specified file, _PHP tutorial
php implements traversing directories and deleting specified content in specified files, I am sitting in a quiet dormitory now, and the winter vacation seems to have left me… What I posted today is the last study I did during the winter vacation, and I have been watching One Piece since then. I used to write a C language directory traversal + file copy program, which was very long and complicated. Now I can use PHP to traverse the directory, and the code is much shorter. The purpose of this program is to traverse the directory, find all files with the specified file name, and delete the specified string. The code is as follows: <?php //Function: Delete the specified string in all specified files in the specified directory (including subdirectories) $tmpfiledir = $_SERVER[“DOCUMENT_ROOT”].’tmp.txt’; function del($getstr) { $isbak = true; //Whether to back up the original file, true means backup, false does not back up global $tmpfiledir; $fr = fopen($tmpfiledir,”r”) or die(‘Failed to open temporary file’); while($row = fgets($fr)) { if(empty($row)) break; $row = trim($row); $opp = fopen($row,”r”) or die(“Failed to open $row”); $str = fread($opp,filesize($row)) or die(“Cannot read $row”); $str = str_replace($getstr,””,$str); fclose($opp); if($isbak){ copy($row,$row.’.bak’) or die(“Backup file failed”); } $ref…
PHP simulates the method for users to automatically publish articles in QQ space, _PHP tutorial
How PHP simulates users to automatically publish articles in QQ space, The example in this article describes how PHP simulates users to automatically publish articles in QQ space. Share it with everyone for your reference. The specific analysis is as follows: What we have here is a simple program that uses PHP to simulate logging in and then sending articles to QQ space. Friends in need can refer to it or give me suggestions for improvement. The code is as follows: The code is as follows: <?php //Simulate get post request function /* Function description: Function: The request method can be get, post, COOKIE can be sent, and the COOKIEfile file can be saved Parameters: $url—–request url $referer—source url $postdata———-data for post request, ” is for get request $COOKIE—–COOKIE sent $COOKIEfile—–COOKIEfile saved Return value: Return the obtained source code */ function request($url,$referer=”,$postdata=”,$COOKIE=”,$COOKIEfile=”){ //header settings $header=”; $header.=”Content-Type: application/x-www-form-urlencodedrn”;//Content request type Media Center PC 6.0)rn”;//Browser field $header.=”Referer:”.$referer.”rn”;//Set the source address $header .= “COOKIE:”.$COOKIE; //Set COOKIE, empty by default //Request method get post, pass $postdata empty—get, non-empty—-post if($postdata==”)$method=’GET’; else $method=’POST’; //Define the array used to create the stream $opts=array(); $opts[‘http’]=array(‘method’=>$method,’header’=>$header,’content’=>$postdata); //Generate stream $cOntext=stream_context_create($opts); //Send a request and get the source code $yuanma=file_get_contents($url,false,$context); //Whether…
Graphical tutorial on installing multiple versions of PHP, mysql, and apache under WampServer, _PHP tutorial
Graphic tutorial for installing multiple versions of PHP, mysql, and apache under WampServer, Today, when I was debugging a PHP program, some functions used in the program could not be used in lower versions, so I had to build a higher version of PHP. As a web developer, it is sometimes necessary to install different versions of php, apache and mysql on your machine. The following is a screenshot after I successfully configured it Different versions of PHP: Different versions of Apache: Here I will use the installation of php5.3.10 as an example to explain How to operate: Left-click on the tray icon -> PHP -> Version -> Select Get more…-> After opening the web page, you will see the following screenshot: Then, click on PHP (red framed part) I want to install PHP5.3.1 here. After finding the corresponding version and downloading it, I can install it by replacing it. After the installation is completed, you will find an additional php5.3.10 directory in the wamp installation directory\bin\php\ directory. Restart wamp and you can start again Left click on the tray icon -> PHP -> Version to switch between different php versions. The installation methods of Apache and Mysql are similar,…
How to read the height, width, frame number and background color of a flash file in PHP, _PHP tutorial
How to read the height, width, frame number and background color of a flash file in php, The example in this article describes how PHP reads the height, width, frame number and background color of a flash file. Share it with everyone for your reference. The specific implementation method is as follows: The code is as follows: <? php /* Example: $file = ‘/data/ad_files/5/5.swf’; $flash = new flash(); $flash = $flash->getswfinfo($file); echo ” The width and height of the file are: “.$flash[“width”].”:”.$info[“height”]; echo ” The file version is “.$flash[“version”]; echo ” The number of file frames is “.$flash[“framecount”]; echo ” The file frame rate is “.$flash[“framerate”]; echo ” The file background color is “.$flash[“bgcolor”]; */ class flash { //Whether to return the background color public $need_back_color = false ; //Whether to return the version public $need_version = false ; //Whether to return the frame rate public $need_framerate = false ; //Whether to return the number of frames public $need_framecount = false ; public function __construct() { } public function getswfinfo( $filename ) { if ( file_exists($filename) ) { //echo “File modification time:”.date(“m d y h:i:s.”, filemtime($filename)).” “; } else { //echo “The target file does not exist!”; return array( “error”…
Write a simple PHP folder creation code, _PHP tutorial
Write a simple PHP code to create a folder, It mainly first obtains the path of the current directory, and then determines whether the folder exists. If it does not exist, create it. <?php define('DIR_ROOT', str_replace('\\','/',dirname(__FILE__)));//Get the current file physical path $tmp_file_path = DIR_ROOT.'/tmp/';//Add the path to the tmp directory in the root directory if(is_dir($tmp_file_path)) echo "tmp folder exists!"; else { mkdir($tmp_file_path, 0700);//If the tmp directory does not exist, create it echo "The tmp folder does not exist, the attempt to create it was successful!”;//Whether the creation was successful was not detected echo “The tmp directory is”.$tmp_file_path; } ?> http://www.bkjia.com/PHPjc/938854.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/938854.htmlTechArticleWrite a simple PHP folder creation code, which mainly first obtains the path of the current directory, and then determines whether the folder exists, and creates it if it does not exist. phpdefine(‘DIR_ROOT’, str_replace(‘\\…
Detailed explanation of how to use phplot to generate image classes, _PHP tutorial
Detailed explanation of how to use phplot to generate image classes, The example in this article describes the usage of phplot to generate image classes. Share it with everyone for your reference. The specific analysis is as follows: phplot is an automatically generated class written using PHP’s image function. First of all, let me state that I only know about it. In some original versions, it seems that it still requires various configurations and support, but now it is used It is the php5 series. As long as you understand some commonly used functions, just like when we learn a software, we only need to know how to use it. If you are interested, you can study it in depth, but for most of us For my friends, as long as you can use it, it is fine. After all, it is not commonly used. It is only used when you need to use PHP to draw pictures. So we only need to know how to use it, then we must know the role of its function, just like when we learn discuz, we only need to know how to use it! The download address of phplot is http://www.sourceforge.net/projects/phplot/ You…
How to read and write data in csv files using fputcsv() function in PHP, _PHP tutorial
php uses the fputcsv() function to read and write data from csv files, The example in this article describes how PHP uses the fputcsv() function to read and write data from a csv file. Share it with everyone for your reference. The specific analysis is as follows: The fputcsv() function is used to format data into csv format for writing to files or databases. 1. Write the string into the csv file, the code is as follows: The code is as follows: $test_array = array( array(“111″,”sdfsd”,”sdds”,”43344″,”rrrr”), array(“sssssssss”,”gdfgfd”,”232323″,”wwewe”,”dsfds”), array(“fgfg”,”e4343″,”dsfds”,”w2332″,”xcvxc”), array(“11212″,”2323″,”344343″,”344343″,”rerreer”), array(“fds”,”43344444″,”33333333″,”ttttttt”,”gggggggggggg”), array(“kdfs”,”dsfdsfds”,”wewewe”,”sdsdddddddd”,”wwwwwwwwwww”) ); $file = fopen(“test.csv”,”w”) or die(“Can’t Open test.csv”); foreach($test_array as $line_array) { $isSuccess = fputcsv($file,$line_array); print $isSuccess.”“; if($isSuccess===false) { die(“Can’t write csv line”.$line_array); } } fclose($file) or die(“Can’t close file test.csv.”); The fputcsv() function returns the number of characters in the written line or false. It returns false when the writing fails. 2. Save the formatted csv string into a string, the code is as follows: The code is as follows: $test_array = array( array(“111″,”sdfsd”,”sdds”,”43344″,”rrrr”), array(“sssssssss”,”gdfgfd”,”232323″,”wwewe”,”dsfds”), array(“fgfg”,”e4343″,”dsfds”,”w2332″,”xcvxc”), array(“11212″,”2323″,”344343″,”344343″,”rerreer”), array(“fds”,”43344444″,”33333333″,”ttttttt”,”gggggggggggg”), array(“kdfs”,”dsfdsfds”,”wewewe”,”sdsdddddddd”,”wwwwwwwwwww”) ); ob_start(); $file = fopen(“php://output”,”w”) or die(“Can’t Open php://output”); foreach($test_array as $line_array) { $isSuccess = fputcsv($file,$line_array); if($isSuccess===false) { die(“Can’t write csv line”.$line_array); } } fclose($file) or die(“Can’t close file…
Tips for sharing php static files returning 304, _PHP tutorial
php static files return 304 tips to share, Sometimes some static files (such as pictures) are output by PHP, and you will find that the requests are all 200. It is a waste of resources to request the static files from the server every time. How to make the browser cache the pictures? We need to output 304 in php. We can use HTTP_IF_MODIFIED_SINCE in php combined with etag to do this. Etag does not have a clearly defined format. We can use the md5 value of the file modification time. The code is as follows: The code is as follows: private function _addEtag($file) { $last_modified_time = filemtime($file); $etag = md5_file($file); // always send headers header(“Last-Modified: “.gmdate(“D, d M Y H:i:s”, $last_modified_time).” GMT”); header(“Etag: $etag”); // exit if not modified if (@strtotime($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]) == $last_modified_time || @trim($_SERVER[‘HTTP_IF_NONE_MATCH’]) == $etag) { header(“HTTP/1.1 304 Not Modified”); exit; } } It can be called in the code before static files (such as pictures) are output. http://www.bkjia.com/PHPjc/938846.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/938846.htmlTechArticlephp static files return 304 tips to share. Sometimes some static files (such as pictures) will be output by php, and you will find that the requests are all 200. It is a waste of resources to request static…
PHP static file generation class instance analysis, _PHP tutorial
php static file generation class instance analysis, The example in this article describes the PHP static file generation class. Share it with everyone for your reference. The specific implementation method is as follows: The code is as follows: defined(‘phpjb51’) or die (header(“http/1.1 403 not forbidden”)); class include_createstatic { private $htmlpath = ”; private $path = ”; public $mOnthpath= ”; private $listpath = ”; private $cOntent= ”; private $filename = ”; private $extname = ‘.html’; public function createhtml($type,$desname,$content) { $this->htmlpath = getappinf(‘htmlpath’); if (!file_exists($this->htmlpath)) { @mkdir($this->htmlpath); } $this->path = $this->htmlpath.$this->monthpath.’/’; if (!file_exists($this->path)) { @mkdir($this->path); } $this->listpath = $this->htmlpath.’list/’; if (!file_exists($this->listpath)) { @mkdir($this->listpath); } switch ($type) { case ‘index’: $this->filename = $desname; break; case ‘list’: $this->filename = $this->listpath.$desname; break; case ‘view’: $this->filename = $this->path.$desname; break; } $this->filename .= $this->extname; $this->cOntent= $content; } public function write() { $fp=fopen($this->filename,’wb’); if (!is_writable($this->filename)) { return false; } if (!fwrite($fp,$this->content)) { return false; } fclose($fp); return $this->filename; } } //Method 2 if(file_exists(“./index.htm”))//Check whether the static index.htm file exists { $time=time(); //If the file modification time is different from the current time, direct it to the htm file, otherwise regenerate the htm if(time-filemtime(“./index.htm”)<600) { header(“location:classhtml/main.htm”); } } //Add ob_start() at your beginning; ob_start(); //Homepage content is your dynamic…
PHP method to display formatted file data size, _PHP tutorial
PHP method to implement formatted file data size display, The example in this article describes the method of displaying the data size of formatted files in PHP. Share it with everyone for your reference. The specific analysis is as follows: Sometimes we need to display the size of a certain file or the size of other data on the web page. This number often spans a large span. If the unit is B, it may be a single digit. If it is 1G, it will be a number up to 1073741824. At this time, we need to format it according to the size. For example, if it is less than 1K, it will be displayed in B units. , if it is less than 1M, it will be displayed in KB, if it is less than 1G, it will be displayed in MB, and so on… The formatting function reference is as follows: The code is as follows: //Format size display function formatSize($b,$times=0){ if($b>1024){ $temp=$b/1024; return formatSize($temp,$times+1); }else{ $unit=’B’; switch($times){ case ‘0’:$unit=’B’;break; case ‘1’:$unit=’KB’;break; case ‘2’:$unit=’MB’;break; case ‘3’:$unit=’GB’;break; case ‘4’:$unit=’TB’;break; case ‘5’:$unit=’PB’;break; case ‘6’:$unit=’EB’;break; case ‘7’:$unit=’ZB’;break; default: $unit=’Unknown unit’; } return sprintf(‘%.2f’,$b).$unit; } } Call: The code is as follows:…