473,803 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="c ounter.txt";
$fh=fopen($visi torFile,"r+") or die("could not open.");
echo 'Debug 1: opened to read';
//read from it
$Visited=fread( $fh, filesize($visit orFile)) 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 1962

"Jofio" <j9******@yahoo .com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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="c ounter.txt";
$fh=fopen($visi torFile,"r+") or die("could not open.");
echo 'Debug 1: opened to read';
//read from it
$Visited=fread( $fh, filesize($visit orFile)) 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.tx t");
$hits = file($count_for _page);
$hits[0]++;
$fp = fopen($count_fo r_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($visi torFile,"r+") or die("could not open.");
$Visited=fread( $fh, filesize($visit orFile)) 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.t xt): 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
3548
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 = serial.Serial()
16
3116
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
3798
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
50
5024
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
5
15046
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 occurs. The file contains characters with ASCII codes above 128. But the file is still text (nothing like UTF7/8 or the like). It also might contain + signs. As a result:
27
4626
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 = $doc->loadHTMLFile("./aBasicSearchResult.html"); if ( $res == true ) { $zip = $doc->getElementById('zipRaw_id')->value; if ( 0 != $zip ) {
9
3705
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 appropriately moved further down? From a cursory search of the libc documentation I can't find such a function. Will I have to write it myself? Thanks.
9
3015
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 of the above after the file is loaded into the RichTextBox, and I am trying to speed the process up by doing it in a temp file.
1
6025
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 function which i think has problem // this a simple program about phonebook,it can add items,del items,find items and #include "member.h" #include <fstream> #include <vector>
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.