Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Put this in a class:</p> <pre><code>/** * Given a string ($data) with a file's contents, guess and return the mime type * * Uses the standard unix program /usr/bin/file to handle the magic (pun intended) * * @param string $data */ public static function get_string_mime_type($data) { $file_cmd = '/usr/bin/file --brief --mime-type --no-buffer -'; return rtrim(self::exec_write_read($file_cmd, $data)); } /** * Executes $cmd, writes to $cmd's stdin, then returns what $cmd wrote to stdout */ private static function exec_write_read($cmd, $write, $log_errors = false) { $descriptorspec = array( 0 =&gt; array("pipe", "r"), // stdin is a pipe that $cmd will read from 1 =&gt; array("pipe", "w"), // stdout is a pipe that $cmd will write to 2 =&gt; array("pipe", "w"), // stderr is a pipe that $cmd will write to ); $process = proc_open($cmd, $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 =&gt; writeable handle connected to child stdin // 1 =&gt; readable handle connected to child stdout // 2 =&gt; readable handle connected to child stderr fwrite($pipes[0], $write); fclose($pipes[0]); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); if( $log_errors ){ error_log(stream_get_contents($pipes[2])); } fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $exit_code = proc_close($process); return $output; } else { throw new Exception("Couldn't open $cmd"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload