How to Use File_Get_Contents() to Compare Two Files

Verifying that two files are identical using pure PHP?

If you already have one SHA1 sum, you can simply do:

if ($known_sha1 == sha1_file($new_file))

otherwise

if (filesize($file_a) == filesize($file_b)
&& md5_file($file_a) == md5_file($file_b)
)

Checking file size too, to somewhat prevent a hash collision (which is already very unlikely). Also using MD5 because it's significantly faster than the SHA algorithms (but a little less unique). Use sha1_file() if you want even less chance of a collision.


This is how to exactly compare two files against each other.

This will run considerably slower than a native hash function.

function compareFiles($file_a, $file_b)
{
if (filesize($file_a) != filesize($file_b))
return false;

$chunksize = 4096;

$fp_a = fopen($file_a, 'rb');
$fp_b = fopen($file_b, 'rb');

try
{
while (!feof($fp_a) && !feof($fp_b))
{
$d_a = fread($fp_a, $chunksize);
$d_b = fread($fp_b, $chunksize);
if ($d_a === false || $d_b === false || $d_a !== $d_b)
return false;
}

return true;
}
finally
{
fclose($fp_a);
fclose($fp_b);
}
}

PHP +issue with the file_get_contents and comparing the value

Try this:

$testA = file_get_contents ('files/test.html');

if(strpos($testA, 'OPEN') !== FALSE){
$color1 = '#00800';
$back1 = '#DDFFDD';
}else{
$color1 = '#FF0000';
$back1 = '#FFD9CC';
}

Edit
You can read more about strpos() here.

PHP-file_get_contents: Unable to check two file_get_contents in a single if statement

Please check the Operator Precedence, && is on higher priority so it executes get_file_contents first, then use && and returns to $exchangeRates. Finally, $exchangeRates is the boolean. you should use () correctly in this case:

    if (
($exchangeRates = file_get_contents('https://api.coinbase.com/v2/exchange-rates'))
&&
($data = file_get_contents('https://api.coinbase.com/v2/prices/spot?currency=USD'))
) {

$json1 = json_decode($exchangeRates, true);
$json2 = json_decode($data, true);

return [$json1, $json2];
}

PHP image comparison

Why don't you try comparing MD5 Hash of the two images.

  $md5LocalImg = md5(file_get_contents($script_img));
$md5WebImg = md5(file_get_contents($web_img));
if ( $md5LocalImg == $md5WebImg ){
echo("SAME");
}
else{
echo("DIFFERENT");
}

Difference between file, file_get_contents, and fopen in PHP

The first two, file and file_get_contents are very similar. They both read an entire file, but file reads the file into an array, while file_get_contents reads it into a string. The array returned by file will be separated by newline, but each element will still have the terminating newline attached, so you will still need to watch out for that.

The fopen function does something entirely different—it opens a file descriptor, which functions as a stream to read or write the file. It is a much lower-level function, a simple wrapper around the C fopen function, and simply calling fopen won't do anything but open a stream.

Once you've open a handle to the file, you can use other functions like fread and fwrite to manipulate the data the handle refers to, and once you're done, you will need to close the stream by using fclose. These give you much finer control over the file you are reading, and if you need raw binary data, you may need to use them, but usually you can stick with the higher-level functions.

So, to recap:

  • file — Reads entire file contents into an array of lines.
  • file_get_contents — Reads entire file contents into a string.
  • fopen — Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.


Related Topics



Leave a reply



Submit