0

I want to check if the content of a text file is the same as another, and if it isn't, write one into the other. My code is as follows:

<?php $file = "http://example.com/Song.txt";
$f = fopen($file, "r+");
$line = fgets($f, 1000);
$file1 = "http://example.com/Song1.txt";
$f1 = fopen($file1, "r+");
$line1 = fgets($f1, 1000);
if (!($line == $line1)) {
    fwrite($f1,$line);
    $line1 = $line;
    };
print htmlentities($line1);
?>

The line is being printed out, but the content isn't being written in the file.

Any suggestions about what might be the problem?

BTW: Im using 000webhost. I'm thinking it's the web hosting service but I've checked and there shouldn't be a problem. I've also checked the fwrite function here: http://php.net/manual/es/function.fwrite.php. Please, any help will be very aprecciated.

2 Answers 2

1

What you're doing will only work for files up to 1000 bytes. Also - you're opening the second file, which you want to write to, using "http://", which means that fopen internally will use an HTTP URL wrapper. These are read-only by default. You should fopen the second file using its local path. Or, to make this simpler, you can do this:

$file1 = file_get_contents("/path/to/file1");
$path2 = "/path/to/file2";
$file2 = file_get_contents($path2);
if ($file1 !== $file2)
    file_put_contents($path2, $file1);
Sign up to request clarification or add additional context in comments.

3 Comments

OK! I fixed the path and it worked. There was no need to change the permissions... Thanks to both anyway!
Glad it worked for you, and no worries. However, it is customary to accept one of the solutions that helped you out. Thanks!
Also - if you kept your code, be aware of the 1000-byte limitation (from fgets($f, 1000))
1

When working with files you will want to use PATHS not URLS.
So
$file = "http://example.com/Song.txt"; becomes
$file = "/the/path/to/Song.txt";

Next:

$file1 = '/absolute/path/to/my/first/file.txt';
$file2 = '/absolute/path/to/my/second/file.txt';
$fileContents1 = file_get_contents($file1);
$fileContents2 = file_get_contents($file2);
if (md5($fileContents1) != md5($fileContents2)) {
    // put the contents of file1 in the file2
    file_put_contents($file2, $fileContents1);
}

Also, you should check that your files are writable by the webserver, that is 0666 permission.

6 Comments

Thanks! I will check the 0666 permission.
detail: you don't need 0666 permissions if the php process owner owns (or group-owns) the file and enclosing directory.
@TasosBitsios - why complicate things for him? Let the man set that permission, we don't know what kind of environment he is using :)
OK! I fixed the path and it worked. There was no need to change the permissions... Thanks to both anyway!
no need to complicate his life indeed, but since this could be used as a reference by someone else, strictly speaking "writable by the webserver" !== 0666 permission :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.