Jump to content

Generate dummy files


mofle

Recommended Posts

I'm trying to create a dummy file generator in PHP, but with no luck. I need the generated files to have some kind of scrambled content, and I need to be able to set the size of the generated file, and the extension. I think it has something to do with the fwrite(), but I'm a PHP noob.

 

I would really appreciate some help on this one ;)

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/
Share on other sites

<?php
$ext = '.txt';
$content = 'some stuff here which u generate';
$handle = fopen('somefile' . $ext, 'w'); //open the file and if it doesn't exist, create it
fwrite($handle, $content); //write the content into the file
fclose($handle); //close the resource
?>

 

You can use file_put_contents() instead of the three fopen(), fwrite() and fclose() functions, but that's a personal preference of mine.

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-618564
Share on other sites

Extending Mchl idea, you can generate content and write the file like this:

 

<?php
$str = range('a', 'z');
$size = 1024;
$content = '';
$file = 'somefile.txt';
for($i=0; $i<$size; $i++){
$content .= $str[rand(0, 25)];
}
$handle = fopen($file, 'w');
fwrite($handle, $content);
fclose($handle);
?>

 

The difference is that the above code generates 1024 letters. Hope that clears things out.

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-618612
Share on other sites

Thank you!

 

How would I create content like this?

 

 

£àŒ∑a`O˙BU_WˇøvÂœwç<k‘u

Ø(e_ˇl≥Ωƒ⁄≠!‡âäaLÿÄãO\sè'Eù4¬l˜ÿ(˘C£Rö\ÿARx≤Œ,ÊPjí؆9B¬ˇSZâµ],Q“zrGO?‰¬§∆πõ»ß™)3˘ÓEfiU>FA“—S`⁄å"õÂÇ–£÷u°»VÇNb›¿Jd«ö◊b∞)‚–t¬&≤P[%ú‰ÁÑiT√º“˚gÊØ ö÷»dÔa.~g†^"e±'GÑŸÛAW5Û—mÓsÕÒFéyºlH;¥˘q⁄Êä”Ö´¡òé?RyLT≠ò5`Ì…çp∏äsÔΩ3+°Æ¸M?Ê]

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-618614
Share on other sites

This will generate random ASCII chars. Don't have an idea at this moment on how to do unicode.

 

$content = null; //make sure $content is empty
$size = 1024 //that the number of chars you want to generate

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

 

Using rand(0,255) generates something that's more similar to what you've posted

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-618616
Share on other sites

That madness to even consider that one.......

 

there even a quick way to make the user think that his file was encripted hahaha...

 

 

quick version.........

 

let the user upload the file thay want destroyed to a folder.................

 

now let them download 10sec afther they have uploadded there file a defaut one you have ready........

 

bobs ur uncle all done lol.....

 

 

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-618837
Share on other sites

Here's how I've gotten so far. I'm having some problems with $content.

 

How can I have the random character written to the $content variable?

 

<?php

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

$content = for ($i = 0; $i < $size; $i++) {
    chr(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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-620398
Share on other sites

Thanks, but it doesn't work. Nothing happens when I implement your code.

 

My current code:

 

 

<?php

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

for ($i = 0; $i < $size; $i++) {
    $content .= chr(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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/120076-generate-dummy-files/#findComment-620443
Share on other sites

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.