/    Sign up×
Community /Pin to ProfileBookmark

Limit written file size (fwrite)

I have a simple script which records search terms and writes them to an external file. I would like to limit this file size.

[B]The Use:[/B]
I have a search page where people can search for PDF files. I would like to have a ‘cloud tag’ or list of the most recent terms searched at the bottom of the page.

Here is my script which works beautifully:

[code=php]
<?php

$pattern = “/filetype:(w+)/”; // filteype:(wildcard for word) to grab the file extension along with the word filetype:

if ( $_GET[‘q’] == “” ) {
$term = “”;
} else {
$term = preg_replace(“$pattern”, ”, $_GET[‘q’]); // get rid of the filetype parameter
}

$searched = $term . “, “;
$fopen = fopen(“searched.html”, “a”);
fwrite($fopen, $searched);
fclose($fopen);
?>

[/code]

The above code grabs the search term when the SERP page is opened, and writes it to a file. I will later use phpInclude to put the contents of that file on the bottom of my search engine page.

The problem is that after a million searches, this file will be huge!

[B]Question:[/B]
How can I limit the file size and organize these search terms so that the most recent ones appear on the page?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@themartyJan 21.2011 — Why don't you use a database for this? It's much more flexible
Copy linkTweet thisAlerts:
@donatelloauthorJan 21.2011 — A mySQL database is probably what I'll have to use, although it would be simpler to have my html file with say, 50 lines... each new search would add a line and delete the oldest line...

That's all I really need.

I want to show the last 50 searches.

My PHP file logs each search, but will grow to a gazillion terabytes as is...
Copy linkTweet thisAlerts:
@machete__7_M_5_Jan 21.2011 — If you have each "search" on a line or comma-separated you could count the inputs in the file.

If it's 50 or above, delete the first line and append the new one at the end.

An quick example with comma-separated content

[CODE]$termsArray = explode("," ,$file);

if(count($termsArray) > 49) {
$newArray = array_slice($a,1,49);
$newStr = implode(",", $newArray);
}[/CODE]


.. then just write $newStr and append the new word to $newStr.
Copy linkTweet thisAlerts:
@machete__7_M_5_Jan 21.2011 — 
$newArray = array_slice($a,1,49);
[/QUOTE]


Asch!!! it should be [CODE]$newArray = array_slice($termsArray,1,49);[/CODE]
Copy linkTweet thisAlerts:
@donatelloauthorJan 23.2011 — @machete

Thanks for your help, but I couldn't get that to work, but did find a solution online after a few hours of hunting and testing...

Here is my final working script:

[code=php]
<?php

###########################################################################
### ###
### This function takes two arguments, $fileName and $lineNum ###
### The example here shows how to delete line number 14 from the file ###
### myfile.txt ###
### Example: ###
### ###
### $fileName = "myfile.txt"; ###
### $lineNum = 14 ###
### delLineFromFile($fileName, $lineNum); ###
### Author Kevin Waterson [email protected] ###
### ###
###########################################################################
// the file name, this can be a path also, like /path/to/myfile.txt
$fileName = "searched.html";

// the line to delete
$lineNum = 1;

delLineFromFile($fileName, $lineNum);


function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array

$arr = file($fileName);
}

// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;

// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}

//remove the line
unset($arr["$lineToDelete"]);

// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}

// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line);}


// Capture search term from search box and add it to end of the file BEGIN

$pattern = "/filetype:(w+)/"; // filteype:(wildcard for word) to delete the word "filetype:" along with the file extension

if ( $_GET['q'] == "" ) {
$term = "";
} else {
$term = preg_replace("$pattern", '', $_GET['q']); // get rid of the filetype parameter
}

$searched = "n".$term . ",";
fwrite ($fp, $searched);


// Capture Search term append END


// close the file
fclose($fp);
}


}

?>

[/code]
Copy linkTweet thisAlerts:
@donatelloauthorJan 23.2011 — ...note...

Of course, since I am posting all search text at the bottom of a page, I used the strip tags function to prevent any malicious code injection...

[code=php]echo strip_tags($text);[/code]
×

Success!

Help @donatello spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...