Solved

Get file size

Posted on 2001-08-16
6
251 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 200 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now