• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

filesize always 1

Ive written some code to generate a file to a set size, however everytime it checks the filesize($randFile) it returns 1, even though I can see the file building on my filesystem greater than 1.

echo "Generating File";
$randFile = "myFile_" . rand(0,9999) . ".dat";

$fh = fopen($randFile, 'w') or die("can't open file");
$stringData = chr(rand(1,255));
fwrite($fh, $stringData);
fclose($fh);

while (filesize($randFile) < 100000)
	{
	
	$fh = fopen($randFile, 'a') or die("can't open file");
	$stringData = "";
	for ($x=1;$x<=1000;$x++)
	{
		$stringData = $stringData . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255));	
	}
	fwrite($fh, $stringData);
	fclose($fh);
	}

Open in new window


Any ideas what Im doing wrong?
0
tonelm54
Asked:
tonelm54
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
filesize($randfile) is a function call.  The filesize might be cached?
0
 
Ray PaseurCommented:
Yep.
http://us.php.net/manual/en/function.filesize.php

You may find the PHP.net man pages very useful.  Example, see this:
http://us.php.net/manual/en/function.clearstatcache.php
0
 
Ray PaseurCommented:
Just curious - what is the purpose of this exercise?  It looks like you are opening and closing things over and over again.  Why do you need to do that?  If you can describe what you're trying to get in plain language we may be able to suggest a simpler alternative.  Just a thought...  Best regards, ~Ray
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tonelm54Author Commented:
Im just trying to create a random file to a set size.

The idea is to do some speed tests, create a file copy it to a network location and then copy it back and display the times it took.

Here is my complete code, however Im having issues with the copy command, so Ill open a new question for that!

<?php

if (isset($_GET['testDest'])==false)
{
	echo "No testDest set in GET";
	exit();	
}
set_time_limit(0); 
error_reporting(-1);


echo "Generating File <br> \n";
$randFile = "myFile_" . rand(0,9999) . ".dat";

$fh = fopen($randFile, 'w') or die("can't open file");
$stringData = chr(rand(1,255));
fwrite($fh, $stringData);
fclose($fh);

while (filesize($randFile) < 100000)
	{
	
	$fh = fopen($randFile, 'a') or die("can't open file");
	$stringData = "";
	for ($x=1;$x<=1000;$x++)
	{
		$stringData = $stringData . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255));	
	}
	fwrite($fh, $stringData);
	fclose($fh);
	clearstatcache();
	}
	
	
echo "File generated, giving size of " . filesize($randFile) . "<br> \n";

function copyFile($randFile, $strSource)
	{
	echo "Starting to copy file to at " . date("Y-m-d H:i:s.u") . "<br> \n";
	$startTime = microtime(true);
	copy($randFile,$strSource);
	echo "Completed copying file to at " . date("Y-m-d H:i:s.u") . "<br> \n";
	echo "Time Taken was " . (microtime(true) - $startTime) . "<br> \n";
	



	$tmpFile = "myFile_" . rand(0,9999) . ".tmp";
	
	echo "Starting to copy file from at " . date("Y-m-d H:i:s.u") . "<br> \n";
	$startTime = microtime(true);
	copy($strSource, $tmpFile);
	echo "Completed copying file from at " . date("Y-m-d H:i:s.u") . "<br> \n";
	echo "Time Taken was " . (microtime(true) - $startTime) . "ms <br> \n";
	//unlink($tmpFile);
	//unlink($strSource);
	echo "<br> \n <HR> <br> \n";
	}

//copyFile($randFile, "\\\\172.16.8.7\\Public\\" . $randFile);


echo "Testing " . $_GET['testDest'] . "<BR>\n";
copyFile($randFile, $_GET['testDest'] . $randFile);

set_time_limit(30);

//unlink($randFile);	
?>

Open in new window


0
 
tonelm54Author Commented:
Just encase anyone has any ideas about the above Ive opened the copy issue in http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27022601.html

Thank you
0
 
Ray PaseurCommented:
Thanks for the points.  I'll post an example of how to create a random file of a set size on the other question.  Best regards, ~Ray
0

Featured Post

Technology Partners: 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!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now