1

I need to pass an actual CRLF to a program as an argument.

If I do:

shell_exec('echo "Hello World" >t.txt');

it works fine. But when I do:

shell_exec('echo "Hello 
World" >t.txt');

it breaks. I need a way to pass an actual CRLF as an argument because ffmpeg requires it when using -headers.

I need a solution for Windows/Linux.

Tom
  • 2,962
  • 3
  • 39
  • 69
  • have you tried a simple Batch script? – CMS_95 Nov 03 '14 at 18:27
  • Try `"\r\n"`. Depending on your platform your existing 2 line code may only be a newline. – AbraCadaver Nov 03 '14 at 18:29
  • aside from that maybe regular VBS also what is the difference between the first one and the other one? – CMS_95 Nov 03 '14 at 18:30
  • @CS_STEM The other one has a CRLF after "Hello and when it runs it just shows "Hello and saves nothing to t.txt. VBS and Batch will not work in Linux. – Tom Nov 03 '14 at 18:39
  • @AbraCadaver It's ignoring '\r\n' and "\r\n" is the same as in my example. – Tom Nov 03 '14 at 18:40
  • as far as batch goes after or before the shell_exec try doing the `echo "Hello World" > t.txt` this will save the string to a txt doc named t.txt now if this is a program you can change the directory with `CD "C:\users..." ` for example. and I figured linux had to be different. hope this bit helps – CMS_95 Nov 03 '14 at 18:53
  • @CS_STEM The problem is that a program needs CRLF as a parameter, like this: myprog.exe -one -two -threeCRFL -four -five – Tom Nov 03 '14 at 18:55
  • 1
    @Tom Read accepted answer to [cmd.exe quoted string expansion?](http://superuser.com/questions/475958/cmd-exe-quoted-string-expansion) – JosefZ Nov 03 '14 at 19:35

2 Answers2

2
shell_exec('echo "Hello \n World" >t.txt');

$ cat t.txt
Hello
 World
jherran
  • 3,337
  • 8
  • 37
  • 54
  • Ok, thanks. But on Windows it gives `"Hello \nWorld"` in t.txt. How can I do this on Windows too? – Tom Nov 03 '14 at 18:43
  • thats because windows considers every thing within " " to be a string since /n is included it outputs it like it is part of the text. – CMS_95 Nov 03 '14 at 18:57
2

Try using PHP_EOL instead of the escaped newline and/or carriage return.

<?php
shell_exec('echo "Hello' . PHP_EOL . 'World" >t.txt');
?>

This worked for me on Linux and OS X — I don't have a Win system to try it on.

Also note it may be CMD.EXE implementation of echo that doesn't like the newline -- other programs may deal with it as you expect, so it's worth trying on ffmpeg.

(edit: as Tom notes, windows PowerShell doesn't have the problem that cmd.exe does)

If all you're doing is creating t.txt to use as input to ffmpeg you can create it directly from PHP instead.

Community
  • 1
  • 1
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • Thanks. It is valid for *nix systems, but seems Windows doesn't like CRLF and the only way I found is to use PowerShell on Windows which accepts `n as CRLF in arguments. PHP should have shell_exec alternative for Windows based on ShellExecute or something similar. With ShellExecute CRLF is not a problem. – Tom Nov 03 '14 at 20:52