473,387 Members | 1,388 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,387 software developers and data experts.

fileio in php

a
Dear all,

I just don't know why the counter.txt just contains the last digit and so
can't accumulate more than 9. furthermore, could anybody tell me in case
php.net help doesn't return explanations, what else place i can go for to
check whether I'm using the functions correctly? Thanks a lot

$filename ="counter.txt";

$handle = fopen($filename, 'r');

$content = fread($handle, filesize($filename));
list ($counter) = $content;

fclose($handle);
$handle = fopen($filename, 'w');
$counter = $counter + 1;
fwrite($handle, $counter);
fclose($handle);

Oct 11 '06 #1
1 1256
a wrote:
I just don't know why the counter.txt just contains the last digit and so
can't accumulate more than 9. furthermore, could anybody tell me in case
php.net help doesn't return explanations, what else place i can go for to
check whether I'm using the functions correctly? Thanks a lot

$filename ="counter.txt";

$handle = fopen($filename, 'r');

$content = fread($handle, filesize($filename));
$content is now a string.
list ($counter) = $content;
But you treat it as an array!

What happens is that PHP takes every single character in $content as
each element of the array.
Try this:

<?php
$content = "42\n";
list($one) = $content;
list($two1, $two2) = $content;

echo '$one is ', $one, "<br>\n";
echo '$two1 is ', $two1, ' and $two2 is ', $two2, "<br>\n";
?>

Anyway, $content already has the value that was in the file. If you need
another variable to hold that value, just do

if (is_numeric($content)) {
$counter = (int)$content;
} else {
$counter = 0; // or some other initialization value (1 ??)
}

<snip>

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 11 '06 #2

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

Similar topics

4
by: Layin'_Low | last post by:
i'm havin a problem with this simple program. i need the output of this loop to be written to a file but i dont know how to do it!! this is what i have right now: gclas = raw_input("What is the...
4
by: Erik Paul | last post by:
I got some maybe stupid questions about file output and functions. At first I have to say I'm programming on Linux. The first thing is I couldn't find a function which can tell me microseconds...
0
by: Patrick De Ridder | last post by:
Below an example from "Windows Forms Programming with C#", by Erik Brown (Manning) p. 228. According to me the example is wrong. The message box does not get activated when an erroneous file name...
0
by: Antonio | last post by:
Hi, I am building a windows service that does the following 1) a System.Timers.Timer elapseed event handler will run code at every interval, maybe every 2 to 5 minutes or so (i have not decided...
1
by: Moe | last post by:
I have a winForm control that contains an openFileDialog. I am using this control in a webForm by enbedding it using an <object> tag. I need to access some information about the file on the client....
0
by: Steven Smith | last post by:
Hi guy's I've hit a bit of a sticking point, I'm sure this is simple enough but I just can't get my head around it and I'm looking for a bit of direction. When dealing with FileIO up until now I...
8
by: Vai2000 | last post by:
Hi All, How do I access a shared Drive which requires username and password to access? I need to write a file in a folder within it TIA
3
by: Marquis | last post by:
I am writing a ActiveX dll using Visual C# However , i am facing fileio and environment permission problem. Is there any way to solve this problem? just as signing the dll or other method to...
0
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi All, I have the following: FileIO.FileSystem.CopyDirectory(strFromString, strLocString, FileIO.UIOption.AllDialogs) Q: How do I access the UI that is presented and re-positon the x,y...
1
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
hi all, I have a button (stopcopy) that I want the user to cancel out of a long file copying operation. I'm not clear what I should "adding" in the sub to make this happen besides & return....
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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.