This code will cause a heavy load on the server?

<?php foreach (glob("./records/*.comm") as $filename) { if (!empty($filename)) { echo ("<div class=\"dialog-round\">\n<span class=\"d1\"></span><span class=\"d2\"></span><span class=\"d3\"></span>\n<div>"); $tmpstr=file_get_contents($filename); $author=$tmpstr; $author=substr_replace($author, '', 0, 10); $author=substr_replace($author, '', strpos($author, '|`author`|')); $date=$tmpstr; $date=substr_replace($date, '', 0, strpos($date, '|~date~|')+8); $date=substr_replace($date, '', strpos($date, '|`date`|')); $time=$tmpstr; $time=substr_replace($time, '', 0, strpos($time, '|~time~|')+8); $time=substr_replace($time, '', strpos($time, '|`time`|')); $ip=$tmpstr; $ip=substr_replace($ip, '', 0, strpos($ip, '|~ip~|')+6); $ip=substr_replace($ip, '', strpos($ip, '|`ip`|')); $message=$tmpstr; $message=substr_replace($message, '', 0, strpos($message, '|~message~|')+11); $message=substr_replace($message, '', strpos($message, '|`message`|')); $number=$filename; $number=substr_replace($number, '', 0, 10); $number=substr_replace($number, '', strpos($number, '.')); if ($ip==$_SERVER['REMOTE_ADDR']) echo ("<p class=\"line\" style=\"margin: 0;\"><a href=\"?edit=$number\">Редактировать</a> | <a href=\"?delete=$number\">Удалить</a></p>"); echo ("<p class=\"line\"><b>Автор:</b> $author, $date <b>в</b> $time</p>"); echo ("<p class=\"line2\"><b>Сообщение:</b>\n<br>\n$message"); echo ("\n<hr>Имя файла с комментарием: $number"); echo ("</div>\n<span class=\"d3\"></span><span class=\"d2\"></span><span class=\"d1\"></span>\n</div><br>"); } } ?> 
  • one
    The load is large, compared to the case, if you had written the "human" code. For the server, the load is most likely small ... - timka_s
  • one
    Just try to think when you write ... You have the content of the same file 5 times in your code, why? Put the file text into a variable and work with it ... - Zowie
  • one
    Well, it is clear that you are learning , this is not a complaint, but just a piece of advice on how to do better - Zowie
  • one
    BUT! The file format and the idea of ​​using a bunch of files - I would review it ... If you are not going to edit files manually - I would use serialize () ... - timka_s
  • 2
    @ Nikita Rabykin, the configuration in a text file is correct. I do not know how in PHP, and in many scripting languages ​​it is convenient to make a configuration file that is written in the same language (usually it consists of "assignment statements") and the executable in the current context sets the "global" variables. In fact, this approach (execution by a certain interpreter of the configuration file) is applicable (with some effort) in many languages. - avp

2 answers 2

The idea of ​​repeatedly reading the same file is flawed.

Read once (in $ tmpstr), assign the changes immediately to $ author, $ date, ... and make repeated changes to them already.

  • @avp, thanks for the advice, I'll try now. - bitrixhater

Your code - I would rewrite it like this:

 <?php foreach ( glob("./records/*.comm") as $filename ){ if ( !empty( $filename ) ) { preg_match( '/^(.*)|~autor~|(.*)|~date~|(.*)|~time~|(.*)|~ip~|(.*)|~message~|(.*)$/', file_get_contents( $filename ), $res ); list( $number, $autor, $date, $time, $ip, $message ) = $res; if ( $ip !== $_SERVER['REMOTE_ADDR'] ) $edit = ''; else { $edit = ''. '<p class = "line" style = "margin: 0;">'. '<a href = "?edit='.$number.'">Редактировать</a> | '. '<a href = "?delete='.$number.'">Удалить</a>'. '</p>'; } echo ''. '<div class = "dialog-round">'. '<span class = "d1"></span>'. '<span class = "d2"></span>'. '<span class = "d3"></span>'. '<div>'. $edit. '<p class = "line"><b>Автор:</b> '.$author.' ('.$date.' '.$time.')</p>'. '<p class = "line2">'. '<b>Сообщение:</b><br>'.$message.'<br>'. '<hr>Имя файла с комментарием: '.$number. '</p>'. '</div>'. '<span class = "d3"></span>'. '<span class = "d2"></span>'. '<span class = "d1"></span>'. '</div><br>'; } } ?> 
  • Thanks, of course, but I am very little familiar with regular expressions, and more specifically, I am not familiar at all. Although your code is much smaller and more “readable”, I, nevertheless, will use which I have, as I can at any moment add something there and delete in 2 accounts, but I will not sit on different Yashi and “Gosh” in search of his answer to the question :) But in any case, thank you very much for the answer. - bitrixhater
  • @ Nikita Rabykin, and in vain, with such an approach (unwillingness to understand the normal code), you will be in the novice category for a long time. - Elime