English 中文(简体)
将函数的动态输出与目的文件比较,并重写
原标题:Compare the size of dynamic output of a function to a destination file, and do a rewrite
  • 时间:2012-05-24 15:58:13
  •  标签:
  • php

根据窗体设置,我有一个函数可以写入文本文件,这是一个相当大的窗体。

不久后,我想将函数的输出与单个文件进行比较,只有目标文件与输出不同时才执行(重写文件)。你们猜想,这是一个性能问题。

这是可行的,BTW?

过程是这样的,我填写了一些表格:

  1. 写入单个文件以包含某些“ 特定” 选项

  2. 一些“ 非特定” 选项不一定在文件中写入任何内容 。

窗体随时可以更新,所以文件的内容可能根据不同的选项增长或缩小。

It only needs a rewrite to the file if I am at point #1. When at point #2, nothing should be written.

这就是我尝试过的:

if ($output != file_get_contents($filepath)) {
  // save the data
}

但我觉得执行过程拖得太久了

我在这里发现一个几乎类似的问题: < a href="https://stackoverflow.com/ questions/3060125/can-i-use-files-get-contents-to-compares-to-compares- two-files" >我能用文件_get_contents () 来比较两个文件吗? ,但我的问题不同。我的问题正在将进程的结果与一个已经存在的文件进行比较,而这个文件只是先前过程的结果。如果文件不同,我只能重写它。

No sensitive data on the form, btw. Any hint is very much appreciated.

谢谢 谢谢

最佳回答

要将整个文件与字符串比较( 我想是字符串, 是不是?) 唯一的办法是阅读整个文件并进行比较。 要改进性能, 您可以通过行阅读文件行, 并在第一行停止, 正如我前面所说的 : explosion Pills

如果您的文件真的很大, 您想要进一步提高性能, 您可以做一些散列的东西 :

  • Generate the output, let s say $output.
  • Calculate md5($output) and store in $output_md5.
  • Compare $output_md5 with a stored one, let s say in file output.md5.
  • Are they equal?
    • If yes, do nothing.
    • If not, save $output into output.txt and $output_md5 in output.md5.
问题回答

与其将整个文件装入内存,不如逐行读取它( fgets ),并将其与输入字符串进行比较,也可以逐行比较。你甚至可以像字符逐字符一样小,但我认为这样做太过过分。

您总是可以尝试将其它文章中的 sha1_files($file) 函数与 sha1($string) 函数合并,并检查该函数的等同性 。





相关问题
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 ...

热门标签