Php com в памяти

memory_get_usage

Returns the amount of memory, in bytes, that’s currently being allocated to your PHP script.

Parameters

Set this to true to get total memory allocated from system, including unused pages. If not set or false only the used memory is reported.

Note:

PHP does not track memory that is not allocated by emalloc()

Return Values

Returns the memory amount in bytes.

Examples

Example #1 A memory_get_usage() example

// This is only an example, the numbers below will
// differ depending on your system

echo memory_get_usage () . «\n» ; // 36640

$a = str_repeat ( «Hello» , 4242 );

echo memory_get_usage () . «\n» ; // 57960

echo memory_get_usage () . «\n» ; // 36744

See Also

User Contributed Notes 15 notes

To get the memory usage in KB or MB

function convert ( $size )
$unit =array( ‘b’ , ‘kb’ , ‘mb’ , ‘gb’ , ‘tb’ , ‘pb’ );
return @ round ( $size / pow ( 1024 ,( $i = floor ( log ( $size , 1024 )))), 2 ). ‘ ‘ . $unit [ $i ];
>

echo convert ( memory_get_usage ( true )); // 123 kb
?>

Note, that the official IEC-prefix for kilobyte, megabyte and so on are KiB, MiB, TiB and so on.

At first glance this may sound like «What the hell? Everybody knows, that we mean 1024 not 1000 and the difference is not too big, so what?». But in about 10 years, the size of harddisks (and files on them) reaches the petabyte-limit and then the difference between PB and PiB is magnificent.

Better to get used to it now. 🙂

memory_get_usage() is used to retrieve the memory allocated to PHP only (or your running script). But intuitively, many people expect to get the memory usage of the system, based on the name of the function.

So if you need the overall memory usage, following function might be helpful. If retrieves the memory usage either in percent (without the percent sign) or in bytes by returning an array with free and overall memory of your system. Tested with Windows (7) and Linux (on an Raspberry Pi 2):

// Returns used memory (either in percent (without percent sign) or free and overall in bytes)
function getServerMemoryUsage ( $getPercentage = true )
$memoryTotal = null ;
$memoryFree = null ;

if ( stristr ( PHP_OS , «win» )) // Get total physical memory (this is in bytes)
$cmd = «wmic ComputerSystem get TotalPhysicalMemory» ;
@ exec ( $cmd , $outputTotalPhysicalMemory );

// Get free physical memory (this is in kibibytes!)
$cmd = «wmic OS get FreePhysicalMemory» ;
@ exec ( $cmd , $outputFreePhysicalMemory );

// Find free value
foreach ( $outputFreePhysicalMemory as $line ) if ( $line && preg_match ( «/^3+\$/» , $line )) $memoryFree = $line ;
$memoryFree *= 1024 ; // convert from kibibytes to bytes
break;
>
>
>
>
else
if ( is_readable ( «/proc/meminfo» ))
$stats = @ file_get_contents ( «/proc/meminfo» );

if ( $stats !== false ) // Separate lines
$stats = str_replace (array( «\r\n» , «\n\r» , «\r» ), «\n» , $stats );
$stats = explode ( «\n» , $stats );

// Separate values and find correct lines for total and free mem
foreach ( $stats as $statLine ) $statLineData = explode ( «:» , trim ( $statLine ));

//
// Extract size (TODO: It seems that (at least) the two values for total and free memory have the unit «kB» always. Is this correct?
//

// Total memory
if ( count ( $statLineData ) == 2 && trim ( $statLineData [ 0 ]) == «MemTotal» ) $memoryTotal = trim ( $statLineData [ 1 ]);
$memoryTotal = explode ( » » , $memoryTotal );
$memoryTotal = $memoryTotal [ 0 ];
$memoryTotal *= 1024 ; // convert from kibibytes to bytes
>

// Free memory
if ( count ( $statLineData ) == 2 && trim ( $statLineData [ 0 ]) == «MemFree» ) $memoryFree = trim ( $statLineData [ 1 ]);
$memoryFree = explode ( » » , $memoryFree );
$memoryFree = $memoryFree [ 0 ];
$memoryFree *= 1024 ; // convert from kibibytes to bytes
>
>
>
>
>

if ( is_null ( $memoryTotal ) || is_null ( $memoryFree )) return null ;
> else if ( $getPercentage ) return ( 100 — ( $memoryFree * 100 / $memoryTotal ));
> else return array(
«total» => $memoryTotal ,
«free» => $memoryFree ,
);
>
>
>

function getNiceFileSize ( $bytes , $binaryPrefix = true ) if ( $binaryPrefix ) $unit =array( ‘B’ , ‘KiB’ , ‘MiB’ , ‘GiB’ , ‘TiB’ , ‘PiB’ );
if ( $bytes == 0 ) return ‘0 ‘ . $unit [ 0 ];
return @ round ( $bytes / pow ( 1024 ,( $i = floor ( log ( $bytes , 1024 )))), 2 ) . ‘ ‘ . (isset( $unit [ $i ]) ? $unit [ $i ] : ‘B’ );
> else $unit =array( ‘B’ , ‘KB’ , ‘MB’ , ‘GB’ , ‘TB’ , ‘PB’ );
if ( $bytes == 0 ) return ‘0 ‘ . $unit [ 0 ];
return @ round ( $bytes / pow ( 1000 ,( $i = floor ( log ( $bytes , 1000 )))), 2 ) . ‘ ‘ . (isset( $unit [ $i ]) ? $unit [ $i ] : ‘B’ );
>
>

// Memory usage: 4.55 GiB / 23.91 GiB (19.013557664178%)
$memUsage = getServerMemoryUsage ( false );
echo sprintf ( «Memory usage: %s / %s (%s%%)» ,
getNiceFileSize ( $memUsage [ «total» ] — $memUsage [ «free» ]),
getNiceFileSize ( $memUsage [ «total» ]),
getServerMemoryUsage ( true )
);

?>

The function getNiceFileSize() is not required. Just used to shorten size in bytes.

Источник

COM and .Net (Windows)

If you are trying to get the properties of a Word document opened via COM object, you may need to define some constants in your script like so.

define ( ‘wdPropertyTitle’ , 1 );
define ( ‘wdPropertySubject’ , 2 );
define ( ‘wdPropertyAuthor’ , 3 );
define ( ‘wdPropertyKeywords’ , 4 );
define ( ‘wdPropertyComments’ , 5 );
define ( ‘wdPropertyTemplate’ , 6 );
define ( ‘wdPropertyLastAuthor’ , 7 );

$word = new COM ( «word.application» ) or die ( «Could not initialise MS Word object.» );
$word -> Documents -> Open ( realpath ( «Sample.doc» ));
$Author = $word -> ActiveDocument -> BuiltInDocumentProperties ( wdPropertyAuthor );

If you are seeing this in your error log:

Fatal error: Class ‘COM’ not found

You require this in php.ini:

Previously it was compiled as built-in on the Windows build. I assume this has happened because the com extension can now be built as a shared component.

When you work with MS Excel, Word, . and other applications, never forget that COM doesn’t know their predefined constants, thus if you want to do sth. that would look like this in VB:

With myRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

you should either use numbers or define constants above, like this:

define ( ‘xlEdgeBottom’ , 9 );
define ( ‘xlContinuous’ , 1 );
define ( ‘xlThin’ , 2 );
define ( ‘xlAutomatic’ , — 4105 );

$ex = new COM ( «Excel.Application» , NULL , CP_UTF8 ) or Die ( «Did not instantiate Excel» );
$wb = $ex -> Application -> Workbooks -> Add ();
$ws = $wb -> Worksheets ( 1 );

$bs = $xra -> Borders ( xlEdgeBottom );
$bs -> LineStyle = xlContinuous ;
$bs -> Weight = xlThin ;
$bs -> ColorIndex = xlAutomatic ;

?>

It is pointless to try to use text strings, i.e.

$bs = $xra -> Borders ( ‘xlEdgeBottom’ );
$bs -> Weight = ‘xlThin’ ;

?>

this will cause an error: Unable to set property Weight of class Border .

Extracting text from Word Documents via PHP and COM

$word = new COM ( «word.application» ) or die ( «Could not initialise MS Word object.» );
$word -> Documents -> Open ( realpath ( «Sample.doc» ));

// Extract content.
$content = (string) $word -> ActiveDocument -> Content ;

$word -> ActiveDocument -> Close ( false );

$word -> Quit ();
$word = null ;
unset( $word );
?>

Add hyperlink at a Word Document’s bookmark

// Create COM instance to word
function clsMSWord ( $Visible = false )
<
$this -> handle = new COM ( «word.application» ) or die( «Unable to instanciate Word» );
$this -> handle -> Visible = $Visible ;
>

function WriteHyperlink ( $Bookmark , $Path , $Text )
<
$objBookmark = $this -> handle -> ActiveDocument -> Bookmarks ( $Bookmark );
$range = $objBookmark -> Range ;
$objHyperlink = $this -> handle -> ActiveDocument -> Hyperlinks ;
$objHyperlink -> add ( $range , $Path , «» , «» , $Text );

Here is some helpful advice for people attempting to use COM with Microsoft MapPoint 2006 or 2009 with PHP.

If you are using apache, it MUST be running under the same credentials as a desktop user that has already installed/run mappoint or modifiy the service and select the «Allow Service to Interact with Desktop» option. Otherwise, it won’t work due to the EULA popup having to be accepted. Mappoint 2004 works just fine, this only applies to 2006 and 2009.

For troubleshooting, the error that appears in the System event viewer is:
The server did not register with DCOM within the required timeout.

The error generated by PHP, if you happen to get lucky and let the COM() function timeout by cranking up set_time_limit() timeout is:

$mapoint = new COM ( «MapPoint.Application» ) or die( «Unable to instantiate Mappoint COM object» );

?>
Generates:
Fatal error: Uncaught exception ‘com_exception’ with message ‘Failed to create COM object `MapPoint.Application’: Server execution failed ‘ in [somefile.php]:9 Stack trace: #0 [somefile.php](9): com->com(‘MapPoint.Applic. ‘) #1 thrown in [somefile.php] on line 9

Also, if you have multiple versions of MapPoint installed, you will need to run:
c:\path\to\mappoint\MapPoint.exe /registerserver

(using the correct folder path) to select the right version of the com object to use. Gooood luck!

Источник

Читайте также:  Phpinfo path to php
Оцените статью