Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在另一个文本文件中写入文本文件内容_Php_Text Files_Server Side_Fwrite - Fatal编程技术网

PHP-在另一个文本文件中写入文本文件内容

PHP-在另一个文本文件中写入文本文件内容,php,text-files,server-side,fwrite,Php,Text Files,Server Side,Fwrite,我想检查一个文本文件的内容是否与另一个相同,如果不相同,则将一个写入另一个。我的代码如下: <?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

我想检查一个文本文件的内容是否与另一个相同,如果不相同,则将一个写入另一个。我的代码如下:

<?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);
?>

该行正在打印,但内容未写入文件

对可能出现的问题有什么建议吗

顺便说一句:我正在使用000webhost。我认为这是网络托管服务,但我已经检查过了,应该没有问题。我还检查了此处的
fwrite
函数:。
请注意,任何帮助都将非常详细。

处理文件时,您希望使用路径而不是URL。
所以
$file=”http://example.com/Song.txt";变为
$file=“/the/path/to/Song.txt”

下一步:

$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);
}

此外,您还应检查您的文件是否可由Web服务器写入,即
0666
权限。

您所做的操作仅适用于最多1000字节的文件。另外,您正在使用“http://”打开第二个文件,这意味着fopen内部将使用http URL包装器。默认情况下,这些是只读的。您应该使用第二个文件的本地路径打开该文件。或者,为了简化此操作,您可以执行以下操作:

$file1 = file_get_contents("/path/to/file1");
$path2 = "/path/to/file2";
$file2 = file_get_contents($path2);
if ($file1 !== $file2)
    file_put_contents($path2, $file1);

谢谢我将检查
0666
权限。详细信息:如果php进程所有者拥有(或组拥有)文件和封闭目录,那么您不需要
0666
权限。@TasosBitsios-为什么要让事情复杂化?让那个人设置权限,我们不知道他在使用什么样的环境:)好的!我修好了这条路,它成功了。没有必要更改权限。。。无论如何都要感谢他们!确实没有必要让他的生活复杂化,但因为这可以被其他人用作参考,严格来说“可由Web服务器编写”!==<代码>0666
权限:)好的!我修好了这条路,它成功了。没有必要更改权限。。。无论如何都要感谢他们!很高兴它对你有用,不用担心。然而,习惯上接受帮助您解决问题的解决方案之一。谢谢另外,如果您保留了代码,请注意1000字节的限制(来自fgets($f,1000)),我会这样做。谢谢你的帮助!