English 中文(简体)
什么是从营地读取然后取代档案内容的最佳途径?
原标题:What s the best way to read from and then overwrite file contents in php?

如何以最清洁的方式打开档案,阅读内容,随后根据原始内容将档案内容与一些产出相重叠? 具体来说,我试图打开一份有物品清单(按新路线分列)的档案,处理/处理清单上的物品,从名单上删除最老的N条目,最后将清单重新编入档案。

fopen(<path>,  a+ )
flock(<handle>, LOCK_EX)
fread(<handle>, filesize(<path>))
// process contents and remove old entries
fwrite(<handle>, <contents>)
flock(<handle>, LOCK_UN)
fclose(<handle>)

请注意,我需要锁定档案,以便在多个页面要求中加以保护。 开放时的“轮船”是否会 the? php手册指出,它将把档案拖到零时间,因此,似乎可能阻止我阅读档案中目前的内容。

问题回答

如果卷宗数量太大(即,你可以相信会装上它所赢得的重机的记忆极限),那么最容易的办法是将整个卷宗改成纸面(.file_get_contents(),处理扼杀,并将结果发回档案(file_put_contents()。 这种做法有两个问题:

  • If the file is too large (say, tens or hundreds of megabytes), or the processing is memory-hungry, you re going to run out of memory (even more so when you have multiple instances of the thing running).
  • The operation is destructive; when the saving fails halfway through, you lose all your original data.

如果有人担心,计划B将处理档案,同时把文件写到临时档案中;在成功完成后,两个档案都关闭,更名为(或删除)原始档案,然后将临时档案改名为原始档案。

阅读

$data = file_get_contents($filename);

撰 写

file_put_contents($filename, $data);

One solution is to use a separate lock file to control access.

这一解决办法假定,只有你的文字或你能够查阅的文字才能书写。 这是因为,这些文字需要知道检查单独的查阅档案。

$file_lock = obtain_file_lock();
if ($file_lock) {
    $old_information = file_get_contents( /path/to/main/file );
    $new_information = update_information_somehow($old_information);
    file_put_contents( /path/to/main/file , $new_information);
    release_file_lock($file_lock);
}

function obtain_file_lock() {

    $attempts = 10;
    // There are probably better ways of dealing with waiting for a file
    // lock but this shows the principle of dealing with the original 
    // question.

    for ($ii = 0; $ii < $attempts; $ii++) {
         $lock_file = fopen( /path/to/lock/file ,  r ); //only need read access
         if (flock($lock_file, LOCK_EX)) {
             return $lock_file;
         } else {
             //give time for other process to release lock
             usleep(100000); //0.1 seconds
         }
    }
    //This is only reached if all attempts fail.
    //Error code here for dealing with that eventuality.
}

function release_file_lock($lock_file) {
    flock($lock_file, LOCK_UN);
    fclose($lock_file);
}

这应防止同时使用的文字阅读旧信息和更新,使你失去另一个文字在阅读档案后更新的信息。 该书只允许一例文字阅读档案,然后用最新信息取代档案。

尽管这一希望回答了原始问题,但它并没有找到一个很好的解决办法,确保所有同时提交的文字能够最终记录其信息。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签