PHP format (file) storage data size (SIZE) display, _PHP tutorial
PHP format (file) storage data size (SIZE) display, Sometimes we need to display the size of a certain file on the web page, or the size of other data. 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: //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: echo formatSize(‘20667564’); The result is: 19.71MB Description: The parameter $b is a number in B, and $times is used to identify how many times this function has been recursed. Please refer to the following remarks (sourced from the Internet) for the…
How to decompress ZIP files online with php, _PHP tutorial
How to decompress ZIP files online with php, The example in this article describes how to decompress ZIP files online with PHP. Share it with everyone for your reference. The specific analysis is as follows: In the PHP function library, I only found a ZLIB function that has something to do with compression. But what disappointed me was that it could not decode ZIP files, but in the end I found a solution, which is through PHP The program execution function is used to implement this function, because there are so many things that can decode ZIP files now. If you don’t believe it, you can look for it where you can download the software. I guarantee you will not be disappointed. My words are not wrong. of. The following is the original file of the program, the upload.php code is as follows: The code is as follows: If it is a *.ZIP file, it will be automatically decompressed The upsave.php code is as follows: The code is as follows: <?php //Save the uploaded file $filename=”$MyFile_name”; copy($MyFile,”$filename”); unlink($MyFile); //Determine whether it is a ZIP file $expand_name=explode(“.”,$filename); if($expand_name[1] == “zip” or $expand_name[1] == “ZIP”) { $str=”pkunzip.exe -e $filename “; exec($str); unlink($filename);…
How to convert text files to csv output in php, _PHP tutorial
php method to convert text file to csv output, The example in this article describes how PHP converts text files into csv output. Share it with everyone for your reference. The specific implementation method is as follows: This class provides a quick, easy way to convert a fixed-width CSV file. It can be used to perform an iteration using a SplFileObject, making it very efficient. An iterator only knows the current member, and options are provided to specify line characters and Field delimiter ends, This from CSV files. This class is particularly useful if data needs to come from a fixed-width file and be inserted into a database, since most databases support data input from CSV files. A convenience feature of this class is that a field can be skipped if not needed in the output, and an array of the fields is provided, providing a key/value pair, with the primary holding the value offset, or starting the field’s status, and the value contains the width, or length of the field, For example. For example, 12=”10 is a field that starts at 12 bits and the width, or length of the field, is 10 characters. The bottom line character of…
PHP uses recursion to calculate folder size, _PHP tutorial
php uses recursion to calculate folder size, The method is very simple, so I won’t go into too much nonsense here, just give you the code: The code is as follows: protected function dir_size($dir){ $dh = opendir($dir); //Open the directory and return a directory stream $size = 0; //Initial size is 0 while(false !== ($file = @readdir($dh))){ //Loop to read files in the directory if($file!=’.’ and $file!=’..’){ $path = $dir.’/’.$file; //Set the directory, used when it contains subdirectories if(is_dir($path)){ $size += $this->dir_size($path); //Recursive call to calculate directory size }elseif(is_file($path)){ $size += filesize($path); //Calculate file size } } } closedir($dh); //Close the directory stream return $size; //return size } http://www.bkjia.com/PHPjc/932493.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/932493.htmlTechArticlephp uses recursion to calculate the folder size. The method is very simple. I won’t go into too much nonsense here. Here is the code: The code is as follows: protected function dir_size($dir){ $dh = opend…
A brief analysis of PHP file downloading principles, _PHP tutorial
A brief analysis of the principles of PHP file downloading, 1. PHP download schematic diagram 2. File download source code: The code is as follows: <?php $file_name=”haha.jpg”;//File to be downloaded $file_name=iconv(“utf-8″,”gb2312″,”$file_name”); $fp=fopen($file_name,”r+”);//To download a file, you must first open the file and write it into the memory if(!file_exists($file_name)){//Determine whether the file exists echo “File does not exist”; exit(); } $file_size=filesize(“a.jpg”);//Judge file size //Returned file Header(“Content-type: application/octet-stream”); //Return in byte format Header(“Accept-Ranges: bytes”); //Return file size Header(“Accept-Length: “.$file_size); //Pop up the client dialog box, corresponding file name Header(“Content-Disposition: attachment; filename=”.$file_name); //Prevent the server from increasing instantaneous pressure and read in segments $buffer=1024; while(!feof($fp)){ $file_data=fread($fp,$buffer); echo $file_data; } //Close the file fclose($fp); ?> 3. Solution to file encoding problem: If the file name is Chinese, PHP’s function cannot recognize the Chinese file name. Generally, if the program encoding is UTF-8, PHP’s function is relatively old and can only recognize Chinese encoded by gb2312, so use iconv(“original encoding” for Chinese). “Encoding to be converted to”, “String to be transcoded”) function can be transcoded. For example, convert a string from utf-8 to gb2312 $file_name=iconv(“utf-8”,”gb2312”,”$file_name”); http://www.bkjia.com/PHPjc/932484.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/932484.htmlTechArticleA brief analysis of the PHP file downloading principle, 1. PHP downloading principle diagram 2. File downloading source code: The…
PHP Chinese coding tips, _PHP tutorial
PHP Chinese coding tips, The problem of Chinese encoding in PHP programming has troubled many people. The reason for this problem is actually very simple. Each country (or region) stipulates the character encoding set for computer information exchange, such as the extended ASCII code of the United States. China’s GB2312-80, Japan’s JIS, etc. As the basis for information processing in this country/region, character encoding sets play an important role in unifying encoding. Character encoding sets are divided into two categories according to length: SBCS (single-byte character set) and DBCS (double-byte character set). In early software (especially operating systems), in order to solve the computer processing of local character information, various localized versions (L10N) appeared. In order to differentiate, concepts such as LANG and Codepage were introduced. However, due to the overlapping code ranges of various local character sets, it is difficult to exchange information with each other; the cost of independent maintenance of each localized version of the software is high. Therefore, it is necessary to extract the commonalities in localization work and process them consistently to minimize special localization processing content. This is also called internationalization (118N). Various language information is further standardized as Locale information. The underlying character…
How to get the real flv file address of youku video with PHP, _PHP tutorial
How to get the real flv file address of youku video in PHP, The example in this article describes how PHP obtains the real flv file address of youku video. Share it with everyone for your reference. The specific analysis is as follows: A webmaster asked me to help him make a real flv address that can automatically test the Youku video website. I sorted it out and solved this problem in an afternoon. It is very good. You can refer to it. This is a borrowed effort, it just captures the content of friends’ websites, but it is quite easy to use. The code is as follows: The code is as follows: <?php $videourl=’http://v.youku.com/v_show/id_XMjA5MjQ0OTQ0.html’; function get_content($url,$data){ if(is_array($data)){ $data = http_build_query($data, ”, ‘&’); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch); return $result; } $str = get_content(‘http://share.pengyou.com/json.php?mod=usershare&act=geturlinfo’,array(‘url’=>$videourl)); $str=json_decode($str); var_dump($str); ?> What came out of this test was only the swf file and not the flv file we wanted. Later, we made improvements based on the writing method of a webmaster. The core code is as follows: The code is as follows: <?php function fetch_youku_flv($url){ preg_match(“#id_(.*?).html#”,$url,$out); $id=$out[1];…
Kindeditor uploads pictures to Qiniu cloud storage plug-in (PHP version), _PHP tutorial
Kindeditor uploads pictures to Qiniu Cloud Storage plug-in (PHP version), Due to work needs, I need to use a third-party storage as a picture bed. I found that Qiniu Cloud is very good and can be used for free. With 10G of space, I decided to try it first. The project uses Kindeditor as the web page editor. There is no ready-made Kindeditor plug-in in Qiniuyun’s plug-in. At first, I wanted to look at the official manual and develop it myself. I don’t know whether it’s because I’m too stupid or not. The manual was too advanced, and I didn’t understand it at all. Later, due to work progress, I almost decided to give up Kindeditor and use Ueditor. After all, there are ready-made plug-ins available. However, due to my enthusiasm for Kindeditor, I finally persisted, even though I couldn’t find Kindeditor. There are too many materials uploaded to Qiniu Cloud, but after seeing the Ueditor version developed by widuu, I decided to develop it myself. After all, many netizens still need this type of plug-in. I would like to thank widuu for developing the Ueditor (PHP) version. During the development process, I referred to his source code and a…
9 classic PHP code snippets to share, _PHP tutorial
9 classic PHP code snippets to share, 1. Check whether the email has been read When you send an email, you may want to know whether the email has been read by the other party. Here’s a very interesting snippet of code that displays the actual date and time the record was read by the other party’s IP address. The code is as follows: <? error_reporting(0); Header(“Content-Type: image/jpeg”); //Get IP if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) { $ip=$_SERVER[‘HTTP_CLIENT_IP’]; } elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) { $ip=$_SERVER[‘HTTP_X_FORWARDED_FOR’]; } else { $ip=$_SERVER[‘REMOTE_ADDR’]; } //Time $actual_time = time(); $actual_day = date(‘Y.m.d’, $actual_time); $actual_day_chart = date(‘d/m/y’, $actual_time); $actual_hour = date(‘H:i:s’, $actual_time); //GET Browser $browser = $_SERVER[‘HTTP_USER_AGENT’]; //LOG $myFile = “log.txt”; $fh = fopen($myFile, ‘a+’); $stringData = $actual_day . ‘ ‘ . $actual_hour . ‘ ‘ . $ip . ‘ ‘ . $browser . ‘ ‘ . “\r\n”; fwrite($fh, $stringData); fclose($fh); //Generate Image (Es. dimesion is 1×1) $newimage = ImageCreate(1,1); $grigio = ImageColorAllocate($newimage,255,255,255); ImageJPEG($newimage); ImageDestroy($newimage); ?> 2. Extract keywords from web pages A great code snippet can easily extract keywords from a web page. The code is as follows: $meta = get_meta_tags(‘http://www.emoticode.net/’); $keywords = $meta[‘keywords’]; // Split keywords $keywords = explode(‘,’, $keywords ); // Trim them $keywords = array_map( ‘trim’, $keywords );…