Current location: Hot Scripts Forums » Programming Languages » PHP » compare the contents of 2 file in php


compare the contents of 2 file in php

Reply
  #1 (permalink)  
Old 04-08-04, 06:48 AM
rani rani is offline
Newbie Coder
 
Join Date: Jan 2004
Location: b'lore
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
compare the contents of 2 file in php

hi
i need to know how to compare the contents of the 2 file in php.
2 files may be a text file or anyother file.
something like diff commnad in linux.
in linux command if we give
diff file1 file2
then show the conents if difference present.
else nothing.
do any such thing present in php too.
if no then how to compare the content of 2 file.
shd we write a code the one in c prg does?
thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-08-04, 08:56 AM
optix's Avatar
optix optix is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

You depending on the content of the files, you could read the contents into an array and us array_diff()

Check it out on php.net: http://us2.php.net/manual/en/function.array-diff.php
__________________
[B] ModernConspiracy.com - Conspiracy Theory Forums (Talk Conspiracy)
DIYAutoForum.com - DIY Auto Forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-08-04, 09:38 AM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rani
hi
i need to know how to compare the contents of the 2 file in php.
If you just want to know whether the two files are the same or not, you could use the following snippet:

Code:
$crc1 = strtoupper(dechex(crc32(file_get_contents($file1))));
$crc2 = strtoupper(dechex(crc32(file_get_contents($file2))));

if ($crc1!=$crc2) {
// files not the same
} else {
// files the same
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-14-04, 06:17 AM
rani rani is offline
Newbie Coder
 
Join Date: Jan 2004
Location: b'lore
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
file_get_contents did not work

hi
i am running php 4.2.2-17 and i think anything >= 4.3 this function works.
i got a error saying
undefined function call file_get_contents in ..

i have a 2 files.
the 2nd file may be a part of the original file or same as the original as original.

the file size ranging from 5.7k to 204 k bytes.

how can i check this file, wh has anything and everything charater, i mean to say that this file has charater, numeric, newline, tab, noncharater also.
so just by putting the value in the array will not work, b'cos each charater has to be check.

can any one help me out.
thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-14-04, 05:49 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
if file_get_contents() doesn't work for you then use the old method ..
PHP Code:

$fp fopen($file1'r');

$content fread($fpfilesize($file1));
fclose($fp);

$fp fopen($file2'r');
$content2 fread($fpfilesize($file2));
fclose($fp);

//USE YourPHPPro script here.. 
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-15-04, 12:47 AM
rani rani is offline
Newbie Coder
 
Join Date: Jan 2004
Location: b'lore
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
thanks

Thanks
i was competely out of my mind,
i didn't really think about this one
thanks a lot.
rani
Quote:
Originally Posted by NeverMind
if file_get_contents() doesn't work for you then use the old method ..
PHP Code:

$fp fopen($file1'r');

$content fread($fpfilesize($file1));
fclose($fp);

$fp fopen($file2'r');
$content2 fread($fpfilesize($file2));
fclose($fp);

//USE YourPHPPro script here.. 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 05:03 AM
PHP write to text file veeco Script Requests 2 02-10-10 03:48 PM
Displaying TEXT file contents to HTML tables Mendo PHP 2 02-18-04 12:19 PM
How to write a php script to load a text file into a table automatically? xmxpcom PHP 2 02-12-04 09:37 AM
php write to file tiny Script Requests 2 01-14-04 10:12 PM


All times are GMT -5. The time now is 06:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.