+

PHP Popen () function

Syntax:
popen (command, mode)
Parameters used:
The popen() function in PHP takes two parameters.
  • command:is a required parameter that specifies the command to be executed.
  • mode:This is a required parameter that specifies the connection mode, for example, read-only (r) or write-only (w).
  • Returned value:
    It returns a file pointer that is identical to the pointer returned by fopen(), but is unidirectional in nature.Errors and Exceptions:
  • The file pointer initiated by the popen() function must be closed with pclose().
  • If the command to execute cannot be found, then the popen() function returns a valid resource.
  • Examples :
    Input: $my_file = popen ("/ bin / ls", "r"); Output: 1 Input: $my_file = popen (`/ executable / gfg.exe`,` r`); echo "` my_file`; ". get_class ($my_handle). ""; $file_read = fread ($my_file, 4192); echo $file_read; pclose ($my_file); Output: 1 
    The programs below illustrate the popen() function.Program 1  
    // open the pipe $my_file = popen ( "/ bin / ls" , "r" ); ?>
    Output:
    1 
    Program 2 // open the pipe $my_file = popen (< / code> `/ executable / gfg.exe` , ` r` );  
    // return the class name of the object using get_class() echo "` $my_file`; "< / code> . get_class ( $my_file ). "" ;  
    // read the file using fread() $filereader = fread ( $my_file , 4192); echo $filereader ;  
    // close the pipe pclose ( $my_file ); ?>
    Output:
    1 
    Related article: PHP | Pclose() function Link:
    http://php.net/manual/en/function.popen.php
    Get Solution for free from DataCamp guru