mofle Posted August 19, 2008 Author Share Posted August 19, 2008 At the end of what? Can you show me using my code? Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620456 Share on other sites More sharing options...
Mchl Posted August 19, 2008 Share Posted August 19, 2008 echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620458 Share on other sites More sharing options...
mofle Posted August 19, 2008 Author Share Posted August 19, 2008 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? Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620461 Share on other sites More sharing options...
Mchl Posted August 19, 2008 Share Posted August 19, 2008 ...Ah yes... didn't notice that... AFAIK if Content-type is set to application/Whatever, browser should try to download the file. Not sure though... Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620464 Share on other sites More sharing options...
mofle Posted August 19, 2008 Author Share Posted August 19, 2008 Before I used your code, the dowload was working, but the file contained only one word, but it worked. Strange. Anyone else know how to fix this? Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620473 Share on other sites More sharing options...
Mchl Posted August 19, 2008 Share Posted August 19, 2008 My echo $content is not needed obviously. But: if ($extension == "doc") where do you set $extension ? Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-620475 Share on other sites More sharing options...
mofle Posted August 20, 2008 Author Share Posted August 20, 2008 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621355 Share on other sites More sharing options...
Mchl Posted August 20, 2008 Share Posted August 20, 2008 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; Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621372 Share on other sites More sharing options...
mofle Posted August 20, 2008 Author Share Posted August 20, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621479 Share on other sites More sharing options...
Mchl Posted August 20, 2008 Share Posted August 20, 2008 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... Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621483 Share on other sites More sharing options...
mofle Posted August 20, 2008 Author Share Posted August 20, 2008 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> Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621502 Share on other sites More sharing options...
Mchl Posted August 20, 2008 Share Posted August 20, 2008 Don't forget to switch off error reporting once you're about to put you script on the web Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621507 Share on other sites More sharing options...
mofle Posted August 20, 2008 Author Share Posted August 20, 2008 Yeah I know, thanks anyway Any response to the questions above? Link to comment https://forums.phpfreaks.com/topic/120076-generate-dummy-files/page/2/#findComment-621512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.