PHP。如果文件写入时没有问题,如何读取文件;a和x2B&引用;,但不能与“一起阅读”;r";?

PHP。如果文件写入时没有问题,如何读取文件;a和x2B&引用;,但不能与“一起阅读”;r";?,php,file-get-contents,fread,file-pointer,Php,File Get Contents,Fread,File Pointer,我有两个脚本:其中一个将变量的值写入文件。在另一个脚本中,我尝试阅读它。它写得没有问题,但不可读。 我在这里写入一个文件: $peer_id=2000000001; $fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt"; $file = fopen($fileLocation,"a+"); fwrite($file, $peer_id); fclose($file); 在这里

我有两个脚本:其中一个将变量的值写入文件。在另一个脚本中,我尝试阅读它。它写得没有问题,但不可读。 我在这里写入一个文件:

$peer_id=2000000001;
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"a+");
fwrite($file, $peer_id);
fclose($file);
在这里,我阅读了文件:

$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt"; 
$file = fopen($fileLocation,"r");
if(file_exists($fileLocation)){
        // Result is TRUE
}
if(is_readable ($file)){
      // Result is FALSE
}
// an empty variables, because the file is not readable
$peer_id = fread($file);
$peer_id = fileread($file);
$peer_id = file_get_contents($file);
fclose($file);

代码在“sprinthost”主机上运行,如果这有区别的话。有人怀疑这是因为托管。

文件获取内容
在短期内,
fopen
fread
fclose
。你不用指针。您只需使用:

$peer_id = file_get_contents($fileLocation);
这对于可读的
也是一样的:

if(is_readable($fileLocation)){
    // Result is FALSE
}
所以完整的代码应该是这样的:

$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
if(file_exists($fileLocation) && is_readable($fileLocation)) {
     $peer_id = file_get_contents($fileLocation);
} else {
    echo 'Error message about file being inaccessible here';
}
文件\u get\u contents
具有用于写入的反向函数。将其与
append
常量一起使用,您应该具有与第一个代码块相同的功能:

file_put_contents($fileLocation, $peer_id, FILE_APPEND | LOCK_EX);

file\u获取内容
在短时间内运行
fopen
fread
fclose
。你不用指针。您只需使用:

$peer_id = file_get_contents($fileLocation);
这对于可读的
也是一样的:

if(is_readable($fileLocation)){
    // Result is FALSE
}
所以完整的代码应该是这样的:

$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
if(file_exists($fileLocation) && is_readable($fileLocation)) {
     $peer_id = file_get_contents($fileLocation);
} else {
    echo 'Error message about file being inaccessible here';
}
文件\u get\u contents
具有用于写入的反向函数。将其与
append
常量一起使用,您应该具有与第一个代码块相同的功能:

file_put_contents($fileLocation, $peer_id, FILE_APPEND | LOCK_EX);

谢谢你帮了大忙!奇怪的是,fread($file),readfile($file)没有读取任何内容…不确定
fread
问题应该与有效指针一起工作。。。但是您使用的功能更多的是用于PHP4,或者在处理可以完全放入内存的大型文件时。如果您可以将
文件放入内存,那么获取内容就更容易使用。对于其他用途,您有
文件读取
读取文件
readfile
进入输出缓冲区,赋值为读取的字节数。如果您使用
fileread
将需要查看该函数,因为它不是本机函数。谢谢!你帮了大忙!奇怪的是,fread($file),readfile($file)没有读取任何内容…不确定
fread
问题应该与有效指针一起工作。。。但是您使用的功能更多的是用于PHP4,或者在处理可以完全放入内存的大型文件时。如果您可以将
文件放入内存,那么获取内容就更容易使用。对于其他用途,您有
文件读取
读取文件
readfile
进入输出缓冲区,赋值为读取的字节数。如果使用
fileread,则需要查看该函数,因为它不是本机函数。