<?php $put = $_POST['file']; $file = fopen("$put","r"); $linesCnt = 0; if(!file) { echo("Ошибка открытия файла"); } else { $num_str = 0; while (!feof($file)) { fgets($file); $num_str++; } echo " <br /> Kolvo strok ($num_str)"; echo " <br /> soderzanie faila: <br />"; echo @fread($file,filesize("$put")); fclose($file); } ?> 

Not implemented part:

 echo @fread($file,filesize("$put")); fclose($file); 

I can not understand why.

  • Remove the dog and you will get an error :) - Zowie

3 answers 3

You read the entire file through fgets in a loop. The reading cursor appears at the end of the file. Then fread will naturally cause an error, since the file is already fully read. It is advisable to write this:

 <? $put = $_POST['file']; $file = fopen("$put","r"); $linesCnt = 0; if(!$file) { echo("Ошибка открытия файла"); } else { $num_str = 0; $strfile = ""; while (!feof($file)) { $strfile .= fgets($file); $num_str++; } echo " <br /> Kolvo strok ($num_str)"; echo " <br /> soderzanie faila: <br />"; echo $strfile; fclose($file); } ?> 
  • With this option, and less memory clogged will be true? - Afimida
  • I just would like to set ini_set ('memory_limit', '128M'); I installed it, it remains to understand how to make this example memory is not scored. - Afimida
  • Memory will be less clogged with this option: echo "<br /> soderzanie faila: <br />"; $ num_str = 0; while (! feof ($ file)) {echo fgets ($ file); $ num_str ++; } echo "<br /> Kolvo strok ($ num_str)"; - vkhacker

Before this part put

 rewind ($file); // или fseek($file, 0); 

You are trying to read data when the pointer at the end of the file is standing.

    if(!file) <- this is a typo, I hope?

    echo @fread($file,filesize("$put")); <- what nonsense. You have already read the file. Make a "buffer" when reading, then just output it:

     $buf = ''; while (!feof($file)) { $buf .= fgets($file); $num_str++; } echo " <br /> Kolvo strok ($num_str)"; echo " <br /> soderzanie faila: <br />"; echo $buf; fclose($file);