Jump to content

Empty text file when there is over XXXX lines of text.


Recommended Posts

define("RANDOM_FILE","/public_html/random.txt");
    $randomEntry = "This goes in text file.\n";
    {
        $randomFile = fopen(RANDOM_FILE,"a");
    }
    fwrite($randomFile,$randomEntry);
    fclose($randomFile);

 

This adds text to a random.txt file. How do you make it also empty the file when there is over say 1,000 lines of data?

 

Each line has 588 bytes of data, or 588K when it has 1,000 lines. How big does a text file need to be before it can slow the site down when the script looks for the random line?

Something like this should work on resetting the file when reached 1000 lines

<?php

$lines = file('path/file.txt');

if(count($lines > 1000)){
file_put_contents('path/file.txt', $randomEntry);
}
else{
file_put_contents('path/file.txt', $randomEntry, FILE_APPEND);
}

?>

How big does a text file need to be before it can slow the site down when the script looks for the random line?

 

That question is not what you meant to ask. Well, a 2kb file will take longer than a 1kb file, but you won't notice the difference. As to when you will notice a difference and, more importantly, when will that performance degredation be to a level that is unacceptable by you is impossible for anyone to determine via a forum.

 

How bug the file will be before you start to have performance issues will depend on several factors: the efficiency of the code to extract the line, where the line of code is in the file, the specifications of the server(s) that the site is running on (CPU, memory, hard-drive speed/capacity), etc. Plus, if you are on a shared host, it will be even more difficult to determine the limits as you won't know what activity is going on for the other sites.

 

The only way to detemine the limits is to run load/performance tests. Ideally, you would want exclusive access to the server. You would then run an application to simulate x number of users on the site performing various activities, where x is the max number of concurrent users you feel you need to support. Then you would increase the allowable size of the file at specific intervals and take recordings until performance degraded to a level that is unacceptable.

Like mjdamato said...it really is dependent on the code you are using to extract the info.  At 588 bytes per line X 1,000 you have a file that is 588,000 bytes large or 574kb or .56Mb.  I have heard that, with efficient coding,  when you get to the 10 Mb size it can slow down the response noticeably.  Just a ball park to work with in the event that you are not (as I am not) sophisticated enough to design and run the testing program the mjdamato was referring to.

 

Cheers-

Thanks. Luckly I'm on a dedicated server so I won't have to worry about getting shut down if it get's too slow!

 

<?
$textfile ="random.txt";
$items = file("$textfile");
$item = rand(0, sizeof($items)-1);
echo $items[$item];
?>

 

is the code. I think it looks through every line, THEN selects a random line!! Hence the file size worry!! Is it possible to make it any more efficient? Each line is

 

<center><object width=500 height=400><param name=movie value=http://www.youtube.com/watch?v=$v&rel=0></param><param name=wmode value=transparent></param><embed src=http://www.youtube.com/watch?v=$v&rel=0 type=application/x-shockwave-flash wmode=transparent width=300 height=250></embed></object><BR><A HREF=http://www.tube-download.info/v/$v/>Download Video</a></form></center><HR>

 

is there any way to make each line just be something like...

 

XX1XX$vXX2XX$vXX3XX$vXX4XX

 

and in the script do a search and replace to add the HTML?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.