1

I'm running this script:

$output = "unrar x -y ".$number.".rar /bundle/";
echo "<pre>";
system($output);
echo "</pre>";

Which is outputting

Extracting /var/www/html/bundle/Compressed_file_test/lgc-m.r29
0% 1% 2% OK

I want to figure out the folder name which it is, in this case "Compressed_file_test". Is there a way i can get that information ?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Exoon
  • 1,513
  • 4
  • 20
  • 35

4 Answers4

3

You could do something like that:

$command = "unrar x -y ".$number.".rar /bundle/";
$output = shell_exec($command);
$output = explode(PHP_EOL, $output);
$output = $output[0];

preg_match_all('/\/[A-Za-z0-9_]+/', $output,  $matches);
echo $matches[1][count($matches[1])-2];

Used functions:

0x9BD0
  • 1,542
  • 18
  • 41
  • Im getting `Warning: preg_match(): Empty regular expression in /var/www/html/bundle/unrar.php on line 14` – Exoon Apr 08 '14 at 19:58
  • $output and the regex are the other way around, regex goes first – Martijn Apr 08 '14 at 19:59
  • Getting this now: Warning: preg_match(): No ending delimiter '/' found in /var/www/html/bundle/unrar.php on line 14 – Exoon Apr 08 '14 at 20:01
  • You are allowed to do some research yourself. Add a slasg before the last single quote in the regex – Martijn Apr 08 '14 at 20:01
  • Yeah i did that but it just came back with "All" assumed i did something wrong. It isn't displaying the progress now it just displays the last line "All Ok" then $matches contains "All" – Exoon Apr 08 '14 at 20:02
2

You want to get the info back intop a variable? You add a 2nd parameter, however, system only returns the last line. Shell?exec returns everything

$result = shell_exec($output);
echo $result;

Documentation on shell_exec()


If you want the value:

 $dir = stubst($output, strpos($output,"/",22), strrpos($output,"/"));
 // or via regex:
 preg_match('/\/(.*?) ?/', $output, $matches);
 $dir = $matches[4];
 // or if you know its the last:
 $dir = end($matches);

That is a simple example, if you get more complex results, you are going to have to change this (you might want to explode on newlines to get induvidual lines)

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • Yeah, just noticed only the last line gets returned. Changed my answer – Martijn Apr 08 '14 at 19:51
  • The second parameter doesn't do what you think it does. See the docs: "*If the return_var argument is present, then the return status of the executed command will be written to this variable.*" – Amal Murali Apr 08 '14 at 19:52
  • I know, I allready changed my answer to a function which does – Martijn Apr 08 '14 at 19:53
  • 1
    Your answer still has the "*You want can a 2nd parameter*" bit. It doesn't make any sense. Please fix all the typos in your answer (for example: "Shell?exec" should be `shell_exec`, `stubst` should be `substr` etc) – Amal Murali Apr 08 '14 at 19:55
  • Should the $dir = substr ? I changed to that but still dosen't give me the actual dir. – Exoon Apr 08 '14 at 19:56
  • it was just an example, you can use a regex or other functions to get the value you need. Note the 22, that could be 4, I dont know if offset is number of matchers, or offset into string – Martijn Apr 08 '14 at 19:59
  • 1
    This worked in the end: preg_match('#/bundle/(.*?)/#', $output, $matches); thanks for pointing me in the right direction. – Exoon Apr 08 '14 at 20:11
0

If you want to use the program output in your code, consider exec() instead of system().

 $command = "unrar x -y $number.rar /bundle/";
 exec($command, $output, $return_var);

Also, make sure $number doesn't contain any special characters (like ;), or escape them.

gog
  • 10,367
  • 2
  • 24
  • 38
0

Something like this:

$output = "unrar x -y ".$number.".rar /bundle/";
echo "<pre>";
system($output,$result);
preg_match(/\/(.*)/, $result, $path);
echo basename($path[0]);
echo "</pre>";
MaveRick
  • 1,181
  • 6
  • 20