dmikester1 Posted January 28, 2010 Share Posted January 28, 2010 I'm trying to create a text file dynamically to be downloaded in PHP. For some reason there is a blank line getting added to the beginning and end of my text file. Can anyone figure out why this is happening? Thanks Mike <?php $filename = "semicolon-delimited.txt"; // Make sure we can't download files above the current directory location. if((strstr($filename, "\.\.")) != false) { die("I'm sorry, you may not download that file."); } $file = str_replace("..", "", $filename); // Extract the type of file which will be sent to the browser as a header $type = filetype($file); // Send file headers header("Content-type: $type"); header("Content-Disposition: attachment;filename=$filename"); header("Content-Transfer-Encoding: ascii"); header('Pragma: no-cache'); header('Expires: 0'); $ourFileHandle = fopen($file, 'w') or die("can't open file"); for($i = 0; $i < 5; $i++) { $stringData = "E;$i;a line;a phrase;"; //. $row['performance'] . ";" . $row['lastName'] . ";" . $row['firstName'] . ";"; $stringData = trim($stringData); fwrite($ourFileHandle, "$stringData\r\n"); } $file = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $file); fclose($ourFileHandle); // Send the file contents. set_time_limit(0); readfile(trim($file)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/190067-blank-lines-getting-added-to-file/ Share on other sites More sharing options...
oni-kun Posted January 28, 2010 Share Posted January 28, 2010 fwrite($ourFileHandle, "$stringData\r\n"); There you go. It obviously would add it to the end line, and the following write would be after that. Quote Link to comment https://forums.phpfreaks.com/topic/190067-blank-lines-getting-added-to-file/#findComment-1002778 Share on other sites More sharing options...
dmikester1 Posted January 28, 2010 Author Share Posted January 28, 2010 Hah, OK that was really dumb of me. Thank you very much oni-kun. Now, I'm also having a problem with a blank line with what looks like 2 tabs getting added at the very beginning. Do you know why that is? Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/190067-blank-lines-getting-added-to-file/#findComment-1002807 Share on other sites More sharing options...
oni-kun Posted January 28, 2010 Share Posted January 28, 2010 Not one hundred percent sure, but if they are tabs then it may be possible your regex is adding them. They shouldn't be there on their own, as they're built by \t and it's not placed by your file code. Quote Link to comment https://forums.phpfreaks.com/topic/190067-blank-lines-getting-added-to-file/#findComment-1002809 Share on other sites More sharing options...
dmikester1 Posted January 28, 2010 Author Share Posted January 28, 2010 Sweet, I figured it out! My included login.php file had some extra lines and tabs/spaces in it. I cleaned that up and it works like a charm! Mike Quote Link to comment https://forums.phpfreaks.com/topic/190067-blank-lines-getting-added-to-file/#findComment-1002831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.