Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Generator

Status
Not open for further replies.

Ruggie

Programmer
Aug 2, 2003
46
US
I was having trouble with an upload script that I had and was getting a 502 Bad Gateway error, I wanted to know if it was a filesize problem or a timeout problem.

Finding files on my computer of the right sizes proved to be too annoying so I wrote a quick script that makes files in increments of 1 meg.

It proved very helpful, turns out I was dealing with a timeout issue and not a file size issue, below is the code if anyone else wants to use it!

FYI, was written for windows based systems and was made to be ran from the command line. I did add some <pre> tags incase it was ever ran from a browser.

Code:
<?
// Adjust these as you see fit
$start_at_megs = 1; // In Megs, Start at 1 Meg
$end_at_megs = 50; // In Megs, Stop at 100 Megs

set_time_limit(7200); // 2 hours
$fileNames = array();
$fileSizes = array(); // in bytes
// Build Array's
for($i=$start_at_megs;$i<=$end_at_megs;$i++){
	$fileNames[] = $i."MB";
	$fileSizes[] = $i*1024*1024;
}

echo "<pre>\r\n\r\n";$output = "";
if (count($fileNames) == count($fileSizes)){
	$count = count($fileNames);
	for($i=0;$i<$count;$i++){
		$fp = @fopen($fileNames[$i].".txt","x"); // Only create new files, don't overwrite old ones.
		if ($fp != false){
			echo $fileNames[$i].".txt created, filling with data ... ";
			$difference = $fileSizes[$i] - strlen($output);
			for($l=1;$l<=$difference;$l++){
				$output .= "1"; // 1 byte of data
			}
			fwrite($fp,$output);
			echo "done!\r\n";
		}else{
			if ($display_as_html) echo "<br>";
			echo $fileNames[$i].".txt already exists ... skipping!\r\n";
		}
		@fclose($fp);
	}
}else{
	echo "Arrays not the same length! \r\n";
	echo "Exiting...\r\n";
}
echo "\r\n</pre>";
?>

Enjoy!

Comments are welcome.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top