?
Solved

Get file size

Posted on 2001-08-16
6
Medium Priority
?
292 Views
Last Modified: 2011-10-03
I've got the following page that lets you append a line of text to a file:

<html>
<head>
<basefont face="Verdana">
</head>
<body bgcolor="white">
<?php
//set file name
$story = "/tmp/mystory.dat";
//open it
$handle = fopen($story, 'a+');
//add a br
$graffiti .= "<br>";
//write string to file handle
fputs($handle, $graffiti);
//close the file fclose($handle); ?>

<form action=story.php method=get>
<input type=text size=30 name=graffiti>
<input type=submit name=submit value="Add your two bits!">
</form>
<?php
//display file if present
if(file_exists($story)) { echo "<b>Current graffiti reads: </b>";
readfile($story); } else { echo "file not found"; } ?>

</font><p>
</body>
</html>

The first problem is: The page is not displaying the newly appended line on submit.
The display part is always lagging one line behind!

The second problem is: How can I get the filesize of mystory.dat and only allow
execution of the script if the filesize is under 200KB?

Thanks for any suggestions! =)

/kimsi
0
Comment
Question by:kimsi
6 Comments
 
LVL 5

Accepted Solution

by:
aksteve earned 800 total points
ID: 6395289
To read yourfile try using this instead:

$fd = fopen ("$story", "r");
while (!feof ($fd)) {
    $buffer = fgets($fd, 4096);
    echo $buffer;
}
fclose ($fd);
     


I believe all you need is this to check file size:

$size = filesize($story);
if ($size < 200000) {
     // do this
}

Combined:

$size = filesize($story);
if ($size < 200000) {
  // do this
  $fd = fopen ("$story", "r");
  while (!feof ($fd)) {
    $buffer = fgets($fd, 4096);
    echo $buffer;
  }
  fclose ($fd);
}
0
 

Author Comment

by:kimsi
ID: 6395368
aksteve -
This method of reading the file seems to have the same effect...

I start with an empty .dat file

I type "Here is my first line" into the input box and hit the button to submit that.

The page reloads, and under "Current graffiti reads:"
--NOTHING HERE!--

I type in "Here is my second line" into the input box and hit the button to submit that.

The page reloads, and under "Current graffiti reads" my FIRST line appears!
Here is my first line

Could the "read" part be executing before the "append" part?

/kimsi

0
 
LVL 5

Expert Comment

by:aksteve
ID: 6395392
Have you tried closing the file before reading it?

It looks as though the close is commented out

//close the file fclose($handle); ?>

It should be:

//close the file
fclose($handle);
?>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:izwiz
ID: 6396014
You need to call rewind() first to set the file pointer to the begining of the file.
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6396088
Yea you can find the file size by the function
int filesize (string filename)

and by getting the file size you can check the condation.

Harry
0
 

Author Comment

by:kimsi
ID: 6398042
You are right! I had the fclose as a part of the comment...

Thanks, this is working great now =)

/kimsi
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question