0

I am building a news site, written in php. Pages on the website, have content that needs to be automatically updated, depending on some information on the website. My idea was to create one .txt file, that will contain all those information, so I can access it via php and modify pages on the site. When user loads the site, PHP script runs and reads whole file into a string, splits it two times and then works with this strings. But, possible problem is that this .txt file can be really big (like 10mb) and with a lot of traffic, it is probably bad idea to read file this big every time user loads a page, so I probably need something faster and better. Possible solution is mysql database, but would it make big difference? Also, are there some tricks to optimize website performance using PHP?

NOTE: Here is script that reads the file:

 <?php
$myfile = fopen("postovi.txt", "r") or die("Unable to open file!");
$smejanje = fread($myfile,filesize("postovi.txt"));
$postovi= explode("<novipost>", $smejanje);

$i = 0;
        $duzina = sizeof($postovi);
        while ($i < $duzina) //duzina = broj postova <ucitava sve postove>
        {
            $postovi[$i] = explode("|", $postovi[$i]);
            $i++;       
        }   

fclose($myfile);

and here is html element that is to be updated with information from that file:

<?php $id++; ?>
        <div class="mali-post-h">
        <a href="<?php echo($postovi[$id][0]); ?>" id="<?php echo("mali-post-link-".$id); ?>">  
        <div class="mali-post" style="background-image: url(<?php echo $postovi[$id][3]; ?>);" id="<?php echo("mali-post-".$id); ?>">
        <div class="mali-post-naslov">
        <p id="<?php echo("mali-post-naslov-".$id); ?>" class="customfont"><?php echo $postovi[$id][2]; ?></p>
        <p style="font-size:10px"> <span style="font-weight: 900; font-size:15px; color: #3366FF;"> <?php echo $znak ?>  </span> <?php 
        $time = strtotime($postovi[$id][4]);
        echo ' pre '.humanTiming($time); ?> </p>
        </div>
        </div>
        </a>
        </div>
pnuts
  • 58,317
  • 11
  • 87
  • 139
Nikola. P
  • 1
  • 2

2 Answers2

0

If you have 10MB of data that needs to be available to every page, I suggest having that data in RAM rather than loading it from any disk-based data source. While MySQL will probably cache the data in RAM, retrieving and formatting data from MySQL is slower than just keeping the processed data in PHP.

For information on how to share memory, see Reading and writing global variables across scripts in PHP

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

I feel that the case for using databases to store and retrieve data has already been made. A text file will only get you so far - probably only useful for very very small tasks. I would recommend reading a book or finding a good tutorial online to learn how to use MySQL correctly in your PHP applications. MySQL is free, although depending on your needs you may need to have it hosted somewhere online.

Once you learn the basics, there are all kinds of strategies you can employ to speed up your queries. For example, you can pull down much more than you need with a single query and then use PHP to sort out the results. Calls to a database server tend to take more time than CPU cycles.

Typel
  • 1,109
  • 1
  • 11
  • 34