2

I'm trying to do this: system("cmd /c C:\test.txt"); I already tried exec("C:\test.txt"), exec('"C:\test.txt"'), but nothing is working, some tries the script only keeps loading and some tries he loads but no returns! I'm thinking that is permissions problem..

Juliano Lima
  • 709
  • 3
  • 10
  • 17
  • First... why? Second, what are you wanting to do with the file? There are a lot of functions to work within files for php, why do you need the command line to do it? – Jon Nov 26 '13 at 00:50
  • `.txt` files aren't typically runnable programs. What does it contain? What is the supposed outcome? – mario Nov 26 '13 at 00:50
  • 2
    `exec('"C:\test.txt"')` will not work because `test.txt` is not an executable. you should specify an executable file and pass `test.txt` to it as a parameter. – Goran Nov 26 '13 at 00:50
  • It's just a example, actually I want to run a batch file.. – Juliano Lima Nov 26 '13 at 08:18

1 Answers1

4

You can create a .bat file and use this:

openfile.bat

start notepad "myfile.txt"
"myshortcut.lnk"
exit

PHP

exec("C:\openfile.bat")

Source: Open text file and program shortcut in Windows batch file

EDIT Unfortunately i cant test this right now but if you want the process to run in the background this might do the trick in both windows and linux:

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 

execInBackground(start /B openfile.bat);

source: http://www.php.net/manual/en/function.exec.php

also try:

exec("start /B C:\openfile.bat");

and I found an other stack question regaring the same: How do you run a .bat file from PHP?

Community
  • 1
  • 1
Lohardt
  • 1,057
  • 1
  • 12
  • 26
  • Didn't work! :x, I want to RUN, EXECUTE a .exe, not in background.. that just doesn't do anothing, I was thinking about use COM or something.. 'cause this can be a permission apache error – Juliano Lima Nov 27 '13 at 10:13
  • Maybe the problem is how you write your filename. http://stackoverflow.com/questions/17458610/cant-run-exe-files-using-exec-in-php – Lohardt Nov 28 '13 at 12:32