Solved

Get file size

Posted on 2001-08-16
6
259 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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