minks
minks

Reputation: 3039

How can I get php to display the entire content of my file?

I have a text file : Topic identified: Sports

I have 2 php files:

file 1:

<?php
    include "topic_detection.php";  
    $text = isset($_POST["text"]) ? $_POST["text"] : '';

    $file = "textfile.txt";
    $outputfile= "outputfile.txt";


    if(!empty($_POST)){
    writetofile($file, $text, "w");
    execpython($file);

    $topic = getoutput($outputfile);
    }

?>

file 2: topic_detection.php

<?php
function writetofile($file, $content, $writeType){
    $fo = fopen($file, $writeType);
    if($fo){
        fwrite($fo,$content);
        fclose($fo);
    }   
}

function execpython($file){
    system("python predict.py $file");

}

function getoutput($file1){
$fh= fopen($file1, 'r');
$theData = fread($fh,1);
fclose($fh);
echo $theData;
return $theData;
}
?>

On trying to get the output of $theData, the only output I receive is a 'T' I am guessing this T is coming from the first letter in the text file. In that case, am I not making the call correctly?

here is my call:

<div align="center"><h4 style="line-height:150%;"><?php echo $topic; ?></h5></div>

Upvotes: 1

Views: 37

Answers (1)

Matt
Matt

Reputation: 5428

You are passing fread a length of 1: $theData = fread($fh,1);

You will always get only the first character.

To read the entire file: (From the PHP docs http://php.net/manual/en/function.fread.php)

$contents = fread($handle, filesize($filename));

You could also use http://php.net/manual/en/function.file-get-contents.php

Upvotes: 2

Related Questions