【问题标题】:Reading File in PHP: File Path Issues [duplicate]在 PHP 中读取文件:文件路径问题 [重复]
【发布时间】:2017-12-17 05:43:23
【问题描述】:

请有人告诉我我在这里做错了什么?我在同一个文件夹中有一个简单的 txt 文件和一个 php 文件,但是用于读取和输出 txt 文件内容的 php 文件无法做到这一点。我猜是文件路径问题,请帮忙。

<?php
    //open target file
    $fp = fopen("sample_file.txt", 'r');
    echo fread($fp, filesize($fp));
    fclose($fp);
?>

错误:[17-Dec-2017 05:28:53 UTC] PHP 警告:fread():长度 参数必须大于 0 在 /home/chuzymat/public_html/file_reader/read_sample.php 在第 6 行

【问题讨论】:

标签: php filepath


【解决方案1】:

尝试:

echo file_get_contents(__DIR__.'/sample_file.txt');

【讨论】:

    【解决方案2】:

    只需使用此代码

     $fp = fopen("sample_file.txt", 'r');
     echo fread($fp,filesize("sample_file.txt"));
     fclose($fp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多