Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get file size

Posted on 2001-08-16
6
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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