SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Generate dummy files

    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

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    PHP Code:
    <?php
    $chars 
    range('a','z');

    $charsMaxIndex sizeof($chars) - 1;

    $size 1024 1024// 1 MB

    $fp fopen('file.txt''w');
    for (
    $i 0$i $size$i++) {
        
    fwrite($fp$chars[rand(0$charsMaxIndex)]);
    }
    fclose($fp);
    ?>

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!

    How would I create content like this?


    &#163;&#224;Œ∑a`O˙BU_Wˇ&#248;v&#194;œw&#231;<k‘u
    &#216;(e_ˇl≥Ωƒ⁄≠!‡&#226;&#228;aL&#255;&#196;&#227;O\s&#232;'E&#249;4&#172;l˜&#255;(˘C&#163;R&#246;\&#255;ARx≤Œ,&#202;Pj&#237;&#216;†9B&#172;ˇSZ&#226;&#181;],Q“zrGO?‰&#172;&#167;∆π&#245;&#187;&#223;™)3˘&#211;EfiU>FA“—S`⁄&#229;"&#245;&#194;&#199;–&#163;&#247;u&#176;&#187;V&#199;Nb›&#191;Jd&#171;&#246;◊b∞)‚–t&#172;&≤P[&#37;&#250;‰&#193;&#209;iT√&#186;“˚g&#202;&#216; &#246;&#247;&#187;d&#212;a.~g †^"e&#177;'G&#209;Ÿ&#219;AW5&#219;—m&#211;s&#213;&#210;F&#233;y&#186;lH;&#165;˘q⁄&#202;&#228;”&#214;&#180;&#161;&#242;&#233;?RyLT≠&#242;5`&#204;…&#231;p∏&#228;s&#212;Ω3+&#176;&#198;&#184;M?&#202;]

  4. #4
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just widen the range of characters you pick from. You can take a look at chr() as well. However, why does it have to look like that? Isn't it just a dummy file? What are you using it for?

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to make something like this one:

    http://www.xnet.se/fd/

    And I need a way to generate unicode character, like the ones above.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See the user comments for the chr() function for unicode versions.

  7. #7
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php

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

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

    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;

    ?>

  8. #8
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $size $_GET['size'];
    $extension $_GET['extension'];
    $filename $_GET['filename'] . "." $extension;
    $content '';
    for (
    $i 0$i $size$i++)
    {
        
    $content .= chr(mt_rand(0255));
    }
    switch (
    $extension)
    {
        case 
    "xls"$ctype="application/vnd.ms-excel";
            break;
        case 
    "doc"$ctype="application/msword";
            break;
        case 
    "txt"$ctype="text/plain";
            break;
        default: 
    $ctype="application/force-download";
    }
    header("Content-Disposition: attachment; filename=".$filename);
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: ".$ctype);
    header("Content-Length: ".$size);
    print 
    $content;
    exit; 
    my mobile portal
    ghiris.ro


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •