printer_list

(No version information available, might be only in CVS)

printer_list — Return an array of printers attached to the server

Описание

array printer_list ( int $enumtype [, string $name [, int $level ]] )

The function enumerates available printers and their capabilities.

Список параметров

enumtype

enumtype must be one of the following predefined constants:

  • PRINTER_ENUM_LOCAL: enumerates the locally installed printers.
  • PRINTER_ENUM_NAME: enumerates the printer of name , can be a server, domain or print provider.
  • PRINTER_ENUM_SHARED: this parameter can't be used alone, it has to be OR'ed with other parameters, i.e. PRINTER_ENUM_LOCAL to detect the locally shared printers.
  • PRINTER_ENUM_DEFAULT: (Win9.x only) enumerates the default printer.
  • PRINTER_ENUM_CONNECTIONS: (WinNT/2000 only) enumerates the printers to which the user has made connections.
  • PRINTER_ENUM_NETWORK: (WinNT/2000 only) enumerates network printers in the computer's domain. Only valid if level is 1.
  • PRINTER_ENUM_REMOTE: (WinNT/2000 only) enumerates network printers and print servers in the computer's domain. Only valid if level is 1.

name

Used with PRINTER_ENUM_NAME.

level

level sets the level of information request. Can be 1,2,4 or 5.

Возвращаемые значения

Return an array of printers.

Примеры

Пример #1 printer_list() example

<?php
/* detect locally shared printer */
var_dump(printer_list(PRINTER_ENUM_LOCAL PRINTER_ENUM_SHARED));
?>

Коментарии

You can still get the "NAME" of printers through the printer_list as only the array keys "DESCRIPTION" and "COMMENT" crash and not the "NAME" key.

$list_printers = printer_list(PRINTER_ENUM_LOCAL |
PRINTER_ENUM_SHARED);

// get the name of the first returned printer
//(you can add array walk function to get all printer names if you wish.. but I am lazy)

$this_printer = $list_printers[0]["NAME"];

// open a connection to your printer
$my_printer = printer_open($this_printer);

.. blah blah

The output of var_dump (as an example)

var_dump( printer_list(PRINTER_ENUM_LOCAL | PRINTER_ENUM_SHARED) );

array(1) {
  [0]=> array(3) {
  ["NAME"]=> string(14) "Canon BJC-4550"
  ["DESCRIPTION"]=> string(30) "Canon BJC-4550,Canon BJC-4550,"
  ["COMMENT"]=> string(0) "This is my funky printer!" } }

As you can see var_dump (under variable functions in the manual) shows and/or retrieves the corrent information but if you try to place into a variable or read the variable it crashes either apache using php4apache.dll or php when you the command line client or cgi.

So as long as you do not get the comment or description it will list your printers.
2003-06-14 14:52:40
http://php5.kiev.ua/manual/ru/function.printer-list.html
Автор:
As saidyjade mentioned below, printer_list crashes Apache when you pound the resulting array too much. I don't know why this happens, somebody should look deeper into the subject...
I have found out, contrary to saidyjade, that also just getting the NAME attribute (and remember, it's case-sensitive! :-) can crash Apache.

Here's what I had:
<?php
   
foreach (printer_list(PRINTER_ENUM_CONNECTIONS) as $printer)
        echo 
"<option value=\"" addslashes(strtoupper($printer["NAME"])) . "\">" strtoupper($printer["NAME"]) . "\n";
?>
This snippet worked alright, whereas
<?php
   
foreach (printer_list(PRINTER_ENUM_CONNECTIONS) as $printer)
        echo 
"<option value=\"" addslashes(strtoupper($printer["NAME"])) . "\"" . ((strtoupper($printer["NAME"])) ? " selected" "") . ">" strtoupper($printer["NAME"]) . "\n";
?>
crashed Apache everytime the page was loaded.

I played around a bit and found this snippet not crashing:
<?php
   
foreach (printer_list(PRINTER_ENUM_CONNECTIONS) as $printer)
    {
       
$curprin strtoupper($printer["NAME"]);
        echo 
"<option value=\"" addslashes($curprin) . "\"" . (($curprin == $user["drucker"]) ? " selected" "") . ">$curprin\n";
    }
?>

So, in essence you just need to be careful about how much you use the array and if necessary define help variables that you can use as much as you want.

Hope this saves somebody some more minutes...
2004-12-06 05:19:51
http://php5.kiev.ua/manual/ru/function.printer-list.html
You can use all the array data, with no crash.
For that you can do this :

$getprt=printer_list(PRINTER_ENUM_LOCAL| PRINTER_ENUM_SHARED );

if ($debugprt){echo "<pre> get prt";
    print_r($getprt);
    echo "</pre>";
    echo count($getprt);
}
$printers = serialize($getprt);
//echo $printers ;
$pp=str_replace _
(";s:11:\"DESCRIPTION\";",";s:4:\"DESC\";",_
str_replace(";s:7:\"COMMENT\";",";s:4:\"COMM\";",$printers));
$printers=unserialize($pp);
if ($debugprt){echo "<pre> pp :";

    print_r($printers);
    echo "</pre>";
    echo count($printers);
}

An that's ok
2005-10-13 16:33:16
http://php5.kiev.ua/manual/ru/function.printer-list.html
printer_list was not working for me and the docs are a little sparse.  So, I took a look at the code for php_printer.dll (which is at http://viewcvs.php.net/viewvc.cgi/pecl/printer/printer.c, BTW) to figure out what was going on.

Basically, this function is a wrapper for the Windows function EnumPrinters.  You can find this function documented in MSDN (which is at http://msdn.microsoft.com).  Do a search for EnumPrinters and you should see the function itself as the first hit in the search list.

There is a lot more information about what the parameters to the function mean and the results returned.  The printer_list function basically copies the enumtype, name and level parameters directly to the Flags, Name and Level parameters of EnumPrinters, calls the function and then copies all of the fields, in the appropriate data structure returned for the level of information requested, back to the results array.
2007-01-30 21:21:34
http://php5.kiev.ua/manual/ru/function.printer-list.html
First, before you do anything, make sure you have a copy of php_printer.dll that is dated later than the first of February, 2007 because there were several parameter handling bugs that were fixed just prior to that date.

Also, beware that the Windows function EnumPrinters often does not return all of the networked printers until someone on the Web server uses the file manager to look at the networked printers, first.  I'm presuming this causes the server to contact the domain controller for the browse list, which is then used to enumerate the printers.  It does not appear that EnumPrinters will do this on its own, although some of the other values for flags may cause it to do so.

Having said all that, the following code should show you both local and networked printers:

if (function_exists("printer_list"))
  {
  $PrintCurr = 0;

  // Get the local printers.
  $PrintDests = printer_list(PRINTER_ENUM_LOCAL);

  if (isset($PrintDests))
    foreach ($PrintDests as $PrintDest)
      $PrinterList[$PrintCurr++] = $PrintDest["NAME"];

  // We should be able to enumerate the remote printers too.  Let's have a
  // shot at it.
  //
  // Start by looking for the remote printers tree.
  $PrintDoms = printer_list(PRINTER_ENUM_NAME); $PrintTree = "";

  if (isset($PrintDoms))
    foreach ($PrintDoms as $PrintDomain)
      {
      if (preg_match("/Remote/i", $PrintDomain["NAME"]))
        { $PrintTree = $PrintDomain["NAME"]; break; }
      }

  // If we found a remote printers tree, we need to enumerate all of the
  // domains/workgroups/nodes within the tree and all the printers attached
  // to those nodes.
  if ($PrintTree != "")
    {
    // Enumerate all of the domains.
    $PrintDoms = printer_list(PRINTER_ENUM_NAME, $PrintTree);

    if (isset($PrintDoms))
      foreach ($PrintDoms as $PrintDomain)
        {
        // Enumerate all of the nodes within the domain.
        $PrintNodes = printer_list(PRINTER_ENUM_NAME, $PrintDomain["NAME"]);

        if (isset($PrintNodes))
          foreach ($PrintNodes as $PrintNode)
            {
            // Enumerate all of the printers within the node.
            $PrintDests = printer_list(PRINTER_ENUM_NAME,
              $PrintNode["NAME"]);

            if (isset($PrintDests))
              foreach ($PrintDests as $PrintDest)
                $PrinterList[$PrintCurr++] = $PrintDest["NAME"];
            }
        }
    }
  }
2007-02-07 01:14:27
http://php5.kiev.ua/manual/ru/function.printer-list.html
You can use AJAX, and everything works fine...

PHP // lsprt.php
<?php
    $getprt
=printer_listPRINTER_ENUM_LOCAL );
    echo 
json_encode($getprt);
?>

HTML
<script>
$.post('lsprt.php', function(data) {
    value = JSON.parse(data);
    for (val in value) {
        $('#asd').append('<option value="'+ value[val]['NAME'] +'">'+ value[val]['NAME'] +'</option>');
    }
});
</script>

<select id="asd"></select>
2013-05-14 17:07:07
http://php5.kiev.ua/manual/ru/function.printer-list.html
Автор:
<?php
$getprt
=printer_list(PRINTER_ENUM_LOCALPRINTER_ENUM_SHARED );
$printers serialize($getprt);
$printers=unserialize($printers);
 
//print_r($printers);
echo '<select name="printers">';
foreach (
$printers as $PrintDest
echo 
"<option value=".$PrintDest["NAME"].">".explode(",",$PrintDest["DESCRIPTION"])[1]."</option>";
echo 
'</select>';
?>
2015-08-25 19:26:40
http://php5.kiev.ua/manual/ru/function.printer-list.html

    Поддержать сайт на родительском проекте КГБ