boen_robot Posted May 13, 2007 Share Posted May 13, 2007 I need to write the output of a console program to a file. For that I use shell_exec() to execute the program and assign the result to a variable and file_put_contents() to save that output.The problem is new lines in the output appear as squares in the file when viewed with notepad. The output is fine in other editors (Wordpad for example).The output is echoed fine (looking at the source code with notepad, there are no squares, only new lines) and even copyed right (which is why I can't show you that square - pasting it in this textbox only results in a new line) but just not saved right.Here's a sample code to show the issue: <?php$file = shell_exec('D:\exec.bat');echo file_put_contents('__test1__.html',$file);?> Where exec.bat is: @echo Offecho somethingecho something elseecho and moreecho and more Any suggestions on making the output file appear right? Link to comment Share on other sites More sharing options...
Lulzim Posted May 13, 2007 Share Posted May 13, 2007 try this shell_exec('D:\exec.bat >outputfile.txt'); results will be saved on outputfile.txt, than just read the file Link to comment Share on other sites More sharing options...
boen_robot Posted May 13, 2007 Author Share Posted May 13, 2007 For the sample I gave, this seems to solve the problem (cool move btw... I've never even heared about this type of thing).However, for my real problem (not this sample), modifying the command line command is not exactly feasable, though doable.Could you suggest any way that does it without modifying the command? Some adjustment to file_put_contents().... another similar function maybe?Thanks for your reply btw. Link to comment Share on other sites More sharing options...
Lulzim Posted May 14, 2007 Share Posted May 14, 2007 I think there is a problem in notepad and the way it renders special characters. Default line separator in windows is CR and LF pairs, bur even when I write \r\n in file Notepad still shows that squares instead of new line. Other editors display the text correctly. cool move btw... I've never even heared about this type of thingthat is very useful, '>' writes the output to a file and '>>' appends the output to a file Link to comment Share on other sites More sharing options...
reportingsjr Posted May 14, 2007 Share Posted May 14, 2007 Thanks for the info lulzim! I have his thing always happen, its just the wrong type of newline. I'm guessing your running this all on windows, but the newline is messing up somehow. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.