Jump to content

Adding filesize with random data for my pages


forzatio

Recommended Posts

Hi there, I currently have some pages that need to be of a minimum size before they can be used.

So for example I have a page that is 20 KB but in order to be "valid" it needs a minimum filesize of 80 KB.

It doesn't mather how that filesize is achieved,  I thought of adding "filesize" to the existing page.

So that filesize would be random data anyway.

I can't find an appropriate solution for this with php atleast with the knowledge I have now.

Link to comment
Share on other sites

First off what is the purpose of requiring certain pages to be a certain size?

The page at first had some CSV or XML data therefore they needed to have a certain filesize, so they could filter files with too little csv/xml data.

Now in this case there is no CSV or XML data required though the minimum filesize requirement is still there.

Link to comment
Share on other sites

I guess my real question was why do you still need the file size requirement?

 

You can write data to a file all day long to make it a particular size, but the question is why do that. I'm sure it isn't the most efficient way to accomplish whatever it is you are looking to do. Are you just trying to eliminate empty files?

Link to comment
Share on other sites

I guess my real question was why do you still need the file size requirement?

 

You can write data to a file all day long to make it a particular size, but the question is why do that. I'm sure it isn't the most efficient way to accomplish whatever it is you are looking to do. Are you just trying to eliminate empty files?

It just needs to have a minimumsize of 80 KB I can't do anything to change this process it's not my server unfortunately.

how would that be possible anyway then ? the code that I posted above is in the very right direction I hoped.

Link to comment
Share on other sites

Well the reason this question is asked as to who would need to have a Minimum Requirment then from what your code looks like the script will then increase the file size by adding whatever extra data lines to it.  Which in all cases will be rejected by the server since it does not match the file type that is being request for upload with unformated and start of file headers or seperators.

 

Basiclly thats a sign that somone is looking for a way to expilot sometihng.  Adding un-formated or un-needed data files to a CSV or XML even if they are formated a certian way makes the helpers here suspisious of the purpose.

 

Since you said it has CSV or XML means whatever else you place in that file will need to be formated aswell and the data you intend to place in such a file to increase its file size more is at question as well.  Since this is a great way to corrupt things in databases.

 

Thats a dangerious subject to be inquiring upon.

 

 

Link to comment
Share on other sites

Okay, no point scooting round the edge - give him what he wants...

 

// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f, $s = 81920)
{
if(!file_exsits($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a'))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}

 

I wrote it in here, so I didn't test it... But the general idea is there...

NB: $f is the filename, $s is the min filesize in bytes...

 

hth...

Link to comment
Share on other sites

Okay, no point scooting round the edge - give him what he wants...

 

// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f, $s = 81920)
{
if(!file_exsits($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a'))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}

 

I wrote it in here, so I didn't test it... But the general idea is there...

NB: $f is the filename, $s is the min filesize in bytes...

 

hth...

Ok thank you for your code, the purpose is innocent and has nothing to do with causing trouble in databases, like someone says above here.

 

Ontopic: I get this error with the code below:  Parse error: parse error, unexpected T_RETURN on line 13

There was a spelling mistake > exsits/ but I fixed that in the code below, still I get the error above.

 

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'currentpage.php', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a'))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}


?>

 

 

Link to comment
Share on other sites

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'currentpage.php', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}


?>

 

Sorry forgot a closing ) on "if(!($fh = fopen($f, 'a'))", the above code is fixed...

Link to comment
Share on other sites

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'currentpage.php', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}


?>

 

Sorry forgot a closing ) on "if(!($fh = fopen($f, 'a'))", the above code is fixed...

 

thank you, when I put the code above in a page called "currentpage.php" and check the filesize of the page I'm still getting a page of 0 KB ?

Link to comment
Share on other sites

It should create currentpage.php of 80 KB in size, though the code is executed but no output.

Anyone might have a helping hand in this one ?

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'currentpage.php', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	for($x = 0; $x < $d; $x++)
	{
		fwrite($fh, 'x');
	}

	fclose($fh);
}
return;
}


?>

Link to comment
Share on other sites

I bet you aren't calling the function... The code works fine for me...

 

Also, I'm pretty sure it would be better to create a variable then write that variable instead of having a hundred write commands...

 

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'file.txt', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	$content = "";
	for($x = 0; $x < $d; $x++)
	{
		$content .= "x";
	}
	fwrite($fh, $content);
	fclose($fh);
}
return;
}

ResizeFile();
?>

Link to comment
Share on other sites

I added this line ResizeFile($f); to call the function , that's how it stands below that doesn't work at all for me.

 

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'file.txt', $s = 81920)
{
ResizeFile($f);

if(!file_exists($f))


return;


$fs = filesize($f);

$d = $s - $fs;

if($d > 0)



{


if(!($fh = fopen($f, 'a')))


return;




$content = "";

for($x = 0; $x < $d; $x++)



{


$content .= "x";


}


fwrite($fh, $content);


fclose($fh);

}

return;
}

ResizeFile();
?>

Link to comment
Share on other sites

I bet you aren't calling the function... The code works fine for me...

 

Also, I'm pretty sure it would be better to create a variable then write that variable instead of having a hundred write commands...

 

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'file.txt', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	$content = "";
	for($x = 0; $x < $d; $x++)
	{
		$content .= "x";
	}
	fwrite($fh, $content);
	fclose($fh);
}
return;
}

ResizeFile();
?>

 

Why don't you like memory?

 

That'll just bloat memory, sure it might be faster to a certain point, until your allocating 8MB of memory just to store 'nothing'... A few writes isn't that slow since theres an internal buffer (this buffer is flushed when it gets x-bytes big or fclose is called)

 

 

Link to comment
Share on other sites

I bet you aren't calling the function... The code works fine for me...

 

Also, I'm pretty sure it would be better to create a variable then write that variable instead of having a hundred write commands...

 

<?php
// Param0 = Filename, Param1 = minsize (in bytes)
function ResizeFile($f = 'file.txt', $s = 81920)
{
if(!file_exists($f))
	return;

$fs = filesize($f);
$d = $s - $fs;
if($d > 0)
{
	if(!($fh = fopen($f, 'a')))
		return;

	$content = "";
	for($x = 0; $x < $d; $x++)
	{
		$content .= "x";
	}
	fwrite($fh, $content);
	fclose($fh);
}
return;
}

ResizeFile();
?>

 

Why don't you like memory?

 

That'll just bloat memory, sure it might be faster to a certain point, until your allocating 8MB of memory just to store 'nothing'... A few writes isn't that slow since theres an internal buffer (this buffer is flushed when it gets x-bytes big or fclose is called)

 

 

I both tried your code and corbin's code but I'm not getting a text file or currentpage.php with that filesize, I'm using the code exactly as it stands there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.