I
I
Ilya2020-04-23 15:45:28
PHP
Ilya, 2020-04-23 15:45:28

The script hangs due to exec() and popen() does not take into account spaces, how to solve?

Hello everyone and good luck to everyone!

popen("start /b C:/Users/il-ne/Desktop/Sublime Text 3.lnk", "r"); //не находит файл (из за пробелов в строке)

exec('"C:/Users/il-ne/Desktop/Sublime Text 3.lnk" ' . dirname (__FILE__) . "/" . $dir . "/file.php");  // выполняется но скрипт зависает 

echo json_encode(array('data1' => true, 'data2' => '/?page=url')); //выполнится только после закрытия программы sublime text


popen('start /b "C:/Users/il-ne/Desktop/Sublime Text 3.lnk"', "r"); //не выполняется (не знаю почему)

exec('"C:/Users/il-ne/Desktop/Sublime Text 3.lnk" ' . dirname (__FILE__) . "/" . $dir . "/file.php");  // выполняется но скрипт зависает 

echo json_encode(array('data1' => true, 'data2' => '/?page=url')); //выполнится только после закрытия программы sublime text


created a shortcut inside the project and gave it a name without spaces
popen('start /b Sublime.lnk', "r"); //выполняется

exec('"Sublime.lnk" ' . dirname (__FILE__) . "/" . $dir . "/file.php");  // выполняется и файл открывается

echo json_encode(array('data1' => true, 'data2' => '/?page=url')); //выполнится после открытия файла


The problem is that if the program is closed and using only exec() the script hangs, but if the program is already open the script functions normally. Tried through different (rules/not rules) in exec() like >NUL etc. does not work, the script always hangs. I rummaged through more than one page in Google / Yashka and did not find a solution, other than how to use popen to open the program, and then use exec to run files in it. But the problem is that spaces are not taken into account in popen () as I did not try. I understand that I'm most likely doing something wrong, but I can't figure out what exactly. I hope for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-23
@iliks

I found this solution on the internet:

echo json_encode(array('data1' => true, 'data2' => '/?page=url'));

$file = 'C:/Users/il-ne/Desktop/Sublime Text 3.lnk';
$args = dirname (__FILE__) . "/" . $dir . "/file.php";
  
pclose(popen("start \"bla\" \"" . $file . "\" " . $args, "r"));

found here: https://www.somacon.com/p395.php
there are a couple more ways to solve this problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question