Jump to content

How to reuse process? (proc_open)


accident

Recommended Posts

Hi I am fairly new to PHP and need help trying to figure out how to reuse a process.

I have PHP running on a linux box, and here is some of my code

 

while (($file = readdir($handle)) !== false)
{
$text = "";
if (filetype("$dir/$file") === "file") 
{
	$extension = strtolower(file_extension($file));	
	$contents = file_get_contents("$dir/$file");
	if ($extension === "pdf")
	{

		$descriptorspec = array(0 => array("pipe", "r"), 
						1 => array("pipe", "w"),  
						2 => array("file", "/tmp/error-output.txt", "a") 
						);

		$process = proc_open('ps2ascii', $descriptorspec, $pipes);

		if (is_resource($process)) 
		{
			fwrite($pipes[0],$contents);
			fclose($pipes[0]);

			while(!feof($pipes[1]))
			{
				$text .= fgets($pipes[1]);
			}
			fclose($pipes[1]);
		}

		proc_close($process);
	}
}
}
closedir($handle);

 

I will sum up what this code does.

It loops through all files in a directory, if it is a PDF it opens up a process that converts the pdf to a text file. (I stripped out some unnecessary code). It then closes the process and proceeds onto the next file.

 

So this application will start up a new process for each file and then close it again... The problem with this is that it is slow (takes about 1 - 1.5 seconds per file, and some of my folders that I want to run this against have a couple thousand PDFs in them)

Is there a way to just open this process once and constantly use it?

I tried putting it outside of the loop but then after one file I am constantly getting errors (saying no valid resource)

 

Any help would be greatly appreciated.

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/54795-how-to-reuse-process-proc_open/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.