Jump to content

Generate dummy files


mofle

Recommended Posts

I've tried that, it only outputs the text in the browser.

 

The text to be in a downloadable file, that's why I'm using:

 

 

if ($extension == "doc") {
	header("Content-type: application/msexcel");
	header("Content-Disposition: attachment; filename=$filename");
	header("Pragma: no-cache");
	header("Expires: 0");
	print "$header\n$content";

}

 

Any ideas?

It now works. Is there a possibility to shorten the code, preferably the header portion, like and array or something?

 

Because I'm going to add a lot more filetypes, and it so much repeated code.

 

 

 

<?php

$size = $_GET['size'];
$filename = $_GET['filename'] . "." . $_GET['extension'];
$extension = $_GET['extension'];
$content = '';

for ($i = 0; $i < $size; $i++) {
    $content .= chr(mt_rand(0, 255));
}

if ($extension == "doc") {
	header("Content-type: application/msexcel");
	header("Content-Disposition: attachment; filename=$filename");
	header("Pragma: no-cache");
	header("Expires: 0");
	print "$header\n$content";

}
elseif ($extension == "txt") {
	header("Content-type: application/msword");
	header("Content-Disposition: attachment; filename=$filename");
	header("Pragma: no-cache");
	header("Expires: 0");
	print "$header\n$content";
}

?>

You should definitely switch to switch http://www.php.net/switch

 

switch($extension) {
case "xls":
header("Content-type: application/msexcel");
break;
case "doc":
case "txt":
header("Content-type: application/msword");  //this will run if extension is eiteher "doc" or "txt"
break;
}
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print $content;

Thanks ;)

 

 

Do I really need the Content-Type header?

 

I tried removing the switch statement bit, and it worked.

 

So what's really the point of the Content-Type header if the browser detect the type based on the extension anyway?

 

And is there a way to make the random text generation faster?

 

And how can I set a max limit on the filesize?

 

 

 

<?php

$size = $_GET['size'];
$extension = $_GET['extension'];
$filename = $_GET['filename'] . "." . $extension;
$content = '';

for ($i = 0; $i < $size; $i++) {
    $content .= chr(mt_rand(0, 255));
}

switch($extension) {
case "xls":
header("Content-type: application/msexcel");
break;

case "doc":
header("Content-type: application/msword");
break;

case "txt":
header("Content-type: application/notepad");
break;
}

header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print $content;

?>

And how can I set a max limit on the filesize?

 

$maxsize = 1024;  //set it as you wish
if ($_GET['size'] > $maxsize) {
$size = $maxsize
} else {
$size= $_GET['size'];
}

 

And is there a way to make the random text generation faster?

 

Perhaps, but not by much IMHO.

 

So what's really the point of the Content-Type header if the browser detect the type based on the extension anyway?

In your case it's probably useless...

This is my new code, working good ;)

 

Is there any way I can simplyify the code any more?

 

I think there can be done something in the $maxsize thing.

 

I am now using kB instead of Bytes in the form, and therefor I have added * 1024 to the code, is there a better way to do this?

 

Like how would I give the user the choice to choose between Bytes, kB, MB in a dropdown in the form?

 

 

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$maxsize = 1048575 * 8; // 1MB * 8 = 8MB
if ($_GET['size'] * 1024 > $maxsize) {
$size = $maxsize;
} else {
$size = $_GET['size'] * 1024;
}

$extension = $_GET['extension'];
$filename = $_GET['filename'] . "." . $extension;
$content = '';

for ($i = 0; $i < $size; $i++) {
    $content .= chr(mt_rand(0, 255));
}

header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print $content;

?>

 

 

 

And here is the html code:

 

  <form action="dummy.php" method="get">
          
        <p><span class="style1">Name: </span>    
          <input name="filename" type="text" size="20" />
          </p>
          
               <p><span class="style1">Extension:</span>        
  <input name="extension" type="text" size="15">
        </p>
          
        <p><span class="style1">Size (kB):</span>        
  <input name="size" type="text" size="15">
        </p>

	<br><br>
	 <input type="image" src="image.png" width="277" height="61" border="0" onclick="return checkFrom(this.form);"><br />
	</form>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.