473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple text file reading codes NOT working

My PHP code to read a simple text file is listed below. What could have
gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
opton of the fread() function. I thought the code was OK.
<?php

$visitorFile="counter.txt";
$fh=fopen($visitorFile,"r+") or die("could not open.");
echo 'Debug 1: opened to read';
//read from it
$Visited=fread($fh, filesize($visitorFile)) or die("Could not
read.");

//Add counter to reflect new count
$Visited ++;
//close file
fclose($fh);
echo "your are visitor $Visitor";
?>
Please help.
Jofio

Oct 17 '05 #1
3 1934

"Jofio" <j9******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
My PHP code to read a simple text file is listed below. What could have
gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
opton of the fread() function. I thought the code was OK.
<?php

$visitorFile="counter.txt";
$fh=fopen($visitorFile,"r+") or die("could not open.");
echo 'Debug 1: opened to read';
//read from it
$Visited=fread($fh, filesize($visitorFile)) or die("Could not
read.");

//Add counter to reflect new count
$Visited ++;
//close file
fclose($fh);
echo "your are visitor $Visitor";
?>


I don't know what system you are on but for fread() the manual states:

On systems which differentiate between binary and text files (i.e. Windows)
the file must be opened with 'b' included in fopen() mode parameter.

$handle = fopen($filename, "rb");

I just use the following code for my counter:

<?php
$count_for_page = ("hitcounter.txt");
$hits = file($count_for_page);
$hits[0]++;
$fp = fopen($count_for_page, "w");
fputs($fp, "$hits[0]");
fclose($fp);
echo $hits[0];
?>

-Don
--
Ever had one of those days where you just felt like:
http://cosmoslair.com/BadDay.html ?
Oct 17 '05 #2
Jofio wrote:
gone wrong here? When I run it, the browser displays: 'COULD NOT Read'

$fh=fopen($visitorFile,"r+") or die("could not open.");
$Visited=fread($fh, filesize($visitorFile)) or die("Could not


Probably has to do with the r+ mode of opening the file. Try r or rb, or
getting the filesize before opening the file.

--
E. Dronkert
Oct 18 '05 #3
My PHP code to read a simple text file is listed below. What could have
gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
opton of the fread() function. I thought the code was OK.


There are many possible reasons. Please ensure that error reporting is
turned on. See
<http://www.php.net/manual/en/function.error-reporting.php>.

ie first statement: error_reporting(E_ALL);

Then you should see a message along the lines of:

PHP Warning: fopen(counter.txt): failed to open stream: No such
file or directory in xxxx on line x

This will show the real reason your fopen() is failing. You might have
to use View / Source to see the error message embedded in your HTML if
you have already written tags.

---
Steve

Oct 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
5
by: Lenard Gunda | last post by:
hi! I have the following problem. I need to read data from a TXT file our company receives. I would use StreamReader, and process it line by line using ReadLine, however, the following problem...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.