ฟังก์ชัน popen() ใช้ เปิด pipe (กระบวนการระหว่างกระบวนการ) เพื่อรันคำสั่งระบบและอ่าน/เขียนข้อมูล ผลลัพธ์คือ file pointer ที่ใช้กับ fgets(), fgetss(), fwrite() ต้องปิดด้วย pclose()
ไวยากรณ์ (Syntax)
popen(command, mode)
พารามิเตอร์ (Parameters)
command − คำสั่งที่ต้องการรัน (เช่น "/bin/ls", "dir")
mode − โหมดการเปิด: "r" (อ่าน), "w" (เขียน)
ค่าที่ส่งคืน (Return Value)
ส่งคืน file pointer เมื่อสำเร็จ, FALSE เมื่อเกิดข้อผิดพลาด
ตัวอย่างการใช้งาน
ตัวอย่างที่ 1: รันคำสั่ง ls บน Linux/Unix
<?php
$file_pointer = popen("/bin/ls", "r");
pclose($file_pointer);
?>
ผลลัพธ์
TRUE
ตัวอย่างที่ 2: รันโปรแกรมติดตั้งบน Windows
<?php
$file_pointer = popen("/executable/installer.exe", "r");
pclose($file_pointer);
?>
ผลลัพธ์
TRUE