当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Append to file...will I need to lock

问题描述:

Php newbie here

I am creating a webpage that stores some information in a text file by appending to it.

Everytime the webpage loads, there is a small php script that adds information to the end of the text file. I am using file_put_contents. Here is VERY simplified version of my code:

<?php

$file = "records.txt";

$current = file_get_contents($file);

$current .= "id = ". $_GET["id"]." \n";

file_put_contents($file, $current );

?>

Here is my concern...if hundreds of people open my webpage, will my script be able to capture ALL the user information without missing anyone. This is extremely important.

I am afraid to lock it(use LOCK_EX) because that would mean that when a new user opens up the webpage the script would not be able to open up and append to the text file if another user is writing to it and thus I would not be able to capture his information which is a BIG problem.

So should I ignore lock or is one needed?? How should I solve this problem

Thanks a lot.

网友答案:

It is not a database but it is similar to locking. You can use a queue to store the messages you want to write to the file. It can become incredibly large though.

网友答案:

Using a database will be very slow. Your problem sounds exactly like what log files do, they do it efficiently, are very well tested and are reliable. Some of them even support sets of files with a file size of your choosing. In theory you would just replace the error message with your data. Although I have not yet used it myself, I see references to log4j all over the place, so it's a good place to start. It uses Java. Java itself has a little logging system built into it that I have used with good results on a small job. You might even be able to treat log4j like a black box and use it for projects built with other languages.

网友答案:

Use fopen() with a switch. This will handle all the problems.

$handle = fopen("somefile.txt", "a");

For your requirements you should not lock the file, but this will probably expose the file to vulnerabilities. So I will suggest an alternative instead.

Instead of a file, insert the information you want on the database.

分享给朋友:
您可能感兴趣的文章:
随机阅读: