fgets not seeing new line - php

my simple php code
<?
error_reporting(E_ALL);
set_time_limit(0);
ini_set('show_errors', 1);
$file = "ftpfile.txt";
$handle = fopen($file, "r");
if(!filesize($file)>0) {
echo "File is empty!";
}
else {
while (($ftpservers = fgets($handle, 4096)) !== false) {
echo $ftpservers. "\n\r";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
my textfile
ftp.server:username:password
ftp.server:username:password
ftp.server:username:password
ect..
running this code returns this
ftp.server:username:passwordftp.server:username:passwordftp.server:username:password
ect..
why is fgets returning the textfile in spades of three?

There is a note about problems with line endings on the fgets() documentation page:
If PHP is not properly recognizing the line endings when
reading files either on or created by a Macintosh computer, enabling the
auto_detect_line_endings run-time configuration option
may help resolve the problem.

just an guess but are you sure the source file contains no spaces between ftp.server:username:password and the next entry?

Related

Reading a big multi GB size text file in php

I have a 197gb text file that I want to read and push the contents into MySql database. I know, I can't put that big file in PHP buffer and read it as whole, So I want to read few hundred lines as a time and keep on reading next and next to read the whole file.
I am trying it with this but the page returns nothing
<?php
$i = 0;
$handle = fopen("./data/200gbfile.txt", "r") or die("Couldn't get handle");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line . "<br />";
if ($i > 100) {
exit;
}
$i++;
}
fclose($handle);
} else {
echo "Error Opeing File!";
}
?>
Is there a limit of the max file size to be handled in php setting?
EDIT: for the 197gb file in question, fopen is failing to return anything and
the output page is just going blank.
You can read the file in chunks to save memory:
For example:
$fd = #fopen("./data/200gbfile.txt", "r");
while (!feof($fd)) {
$data = fread($fd, 1024); // read the file in 1024kb chunks
// handle current data (read line by line for example)
}
fclose($fd);
But no idea if that works with a file with 100Gbytes+.
Edit: # with fopen is required as suggested by Roman.
you can use ini_set('memory_limit','16M'); to set size accordingly but i don't wether it will handle such huge file. never tested that..

PHP - echo and fgets weird characters

I'm trying to display the content of a text file on my website using PHP's fgets, but when I echo the lines in combination with something else (<br>, \n, ...) I get pretty weird characters.
Here's my code :
<?php
header('Content-Type: text/plain;charset=utf-8');
$handle = #fopen("test.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer."<br>";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
Here is the content of test.txt :
1
2
3
4
5
... (6 - 18)
19
20
And here's what I get :
Result with <br>
If I use \n instead of <br>, I don't even get Chinese characters :
Result with \n
I think the issue comes from fgets(), because when I print only one line (without the loop) I get the same issue, but if replace $buffer by "1" (echo "1"."<br>";) I get the expected result.
EDIT
As suggested I modified the code to add header('Content-Type: text/plain;charset=utf-8'); at the beginning of the php file, and modified the output as well.
I found that the issue must be somewhere in the text file : I created a new one and the issue was gone.
I don't know the original encryption of the file because a friend gave it to me.
I'll update this answer if I find out exactly what was going on.
EDIT
I made a copy via TextEdit and when saving it the default encoding format was UTF-16, I guess that was the problem.
Working DEMO: http://phpfiddle.org/main/code/xrsk-a0uv
Text File:: http://m.uploadedit.com/ba3s/1500405331493.txt
Problem: at the Time of create text file it's select the encoding format is UTF-16. !! UTF-8 by default for nodepad,nodepad++,sublime etc.. !!
<?php
header('Content-Type: text/plain;charset=utf-8');
$handle = #fopen("http://m.uploadedit.com/ba3s/1500405331493.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer."</br>";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
NOTE: Add header for charset-utf-8
header('Content-Type: text/plain;charset=utf-8');
OUTPUT Using With "\n"
OUTPUT Using With "</br>"

PEAR Crypt_Blowfish Error Encrypting MP3 File

So, call me crazy (I know you will) but this is only for learning purposes. I'm doing some exploring with php and using PEAR Crypt_Blowfish on a line-by-line read of an mp3 file. It's very server intensive and I'm seeing strange output. I was wondering if anyone had insight as to why:
<?php
include_once '/home/.../php/Crypt/Blowfish.php';
$bf = new Crypt_Blowfish('super');
$handle = #fopen("Judith_full.mp3", "r");
if ($handle) {
while (($buffer = fgets($handle, 1024)) !== false) {
echo $buffer;
echo '<br><br>ENCRYPTED: <br>';
echo $bf->encrypt($buffer).'<br><br>';
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
It's simply showing me the line, then the line encrypted... but part way through the page load I see text that is underlined:
....juMbLEDteXt_thenStartsUnderlinedJuMbLEDteXt_... // can not paste, cause it's encrypted garbage.
The page never full loads, just hangs. Also after the underlining it starts to ignore the <br>'s that are in there.
Thanks to anyone that takes the time.

How to retrieve a variable from http.conf using PHP

How can one retrieve a varibale for instance alias /MyDirectory/ "C:/MyDirectory/MyDirectory/" from http.conf using PHP. Is there an easier way than to open http.conf and read it line by line?
Thanks
You could always use fgets() to read single lines from a file. But why would you want to tinker with your server settings from your program?
$handle = fopen('httpd.conf', 'r');
if($handle) {
while($buffer = fgets($handle) !== false) {
// do something with the data you read
}
if (!feof($handle)) {
echo 'An error occured';
}
fclose($handle);
}

Can I read a .TXT file with PHP?

As I start the process of writing my site in PHP and MySQL, one of the first PHP scripts I've written is a script to initialize my database. Drop/create the database. Drop/create each of the tables. Then load the tables from literals in the script.
That's all working fine! Whoohoo :-)
But I would prefer to read the data from files rather than hard-code them in the PHP script.
I have a couple of books on PHP, but they're all oriented toward web development using MySQL. I can't find anything about reading and writing to ordinary files.
Yes, I know there's a gazillion questions here on stackoverflow about reading TXT files, but when I look at each one, they're for C or C# or VB or Perl. I'm beginning to think that PHP just can't read files :-(
All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:
program readfile;
handle = open('myfile.txt');
data = read (handle);
while (not eof (handle)) begin
display data;
data = read (handle);
end;
close (handle);
end;
I will also need to write files on the server when I get to the part of my site where people upload avatars, and save them as JPG or GIF files. But that's for later.
Thanks!
From the PHP manual for fread():
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
EDIT
per the comment, you can read a file line by line with fgets()
<?php
$handle = #fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:
echo file_get_contents('/path/to/file.txt');
Yes that brief, see file_get_contents, you normally don't need a loop:
$file = new SPLFileObject('/path/to/file.txt');
foreach($file as $line) {
echo $line;
}
Well, since you're asking about resources on the subject, there's a whole book on it in the PHP.net docs.
A basic example:
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Why you not read php documentation about fopen
$file = fopen("source/file.txt","r");
if(!file)
{
echo("ERROR:cant open file");
}
else
{
$buff = fread ($file,filesize("source/file.txt"));
print $buff;
}
file_get_contents does all that for you and returns the text file in a string :)
You want to read line by line? Use fgets.
$handle = #fopen("myfile.txt", "r");
if ($handle) {
while (($content = fgets($handle, 4096)) !== false) {
//echo $content;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}

Categories