Hàm popen() mở một pipe (ống dẫn) tới tiến trình bằng cách fork một lệnh shell. Trả về file pointer có thể dùng với fgets(), fgetss(), fwrite(). Phải đóng bằng pclose().
Cú pháp
popen(command, mode)
Tham số
command – Lệnh shell cần thực thi.
mode – Chế độ: "r" (đọc stdout), "w" (ghi stdin).
Giá trị trả về
Trả về file pointer nếu thành công, FALSE nếu lỗi.
Ví dụ 1: Đọc output lệnh ls (Linux)
<?php
$file_pointer = popen("/bin/ls", "r");
pclose($file_pointer);
?>
Kết quả
TRUE
Ví dụ 2: Chạy file thực thi Windows
<?php
$file_pointer = popen("/executable/installer.exe", "r");
pclose($file_pointer);
?>
Kết quả
TRUE
Lưu ý: popen() chỉ một chiều (read HOẶC write). Cần hai chiều dùng proc_open().