Solved

Get file size

Posted on 2001-08-16
6
235 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

708 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

19 Experts available now in Live!

Get 1:1 Help Now