2015-11-14 10:16

PHP - 比较两个不匹配的文件和错误行


Ok, so I've had a look around and couldn't find anything. I'm using this on a site I am creating as the client is notorious for removing company branding and adding his own.

The index.php file has the following check in it, but I want it to be able to output what lines differ if they are not identical.

$orgf = ''; //Original File
$curf = 'index.php'; //This File
$safe = FALSE;
$error = "";
if (file_get_contents($orgf) == file_get_contents($curf)) { $safe = TRUE; } else { $safe = FALSE; $error = $error."Original index.php file has been modified<br/>"; }

Thanks, Matt.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongyuanguang3893 dongyuanguang3893 6年前

    You could try something like this possibly, though it would be best to use proper paths to the files.

    $differences=array_diff( file( $orgf ), file( $curf ) );
    $safe=( empty( $differences ) ) ? true : false;
    /*Show the differences */
    echo '<pre>',print_r( $differences, true ),'</pre>';

    Because these are 0-indexed arrays, the lines that are different appear to be index+1.

    点赞 评论 复制链接分享