| 1679 | |
| 1680 | /** |
| 1681 | * _set_memory_limit() - Try to set PHP memory limit to amount |
| 1682 | * |
| 1683 | * If the amount is not given, then the default is to set the amount |
| 1684 | * to '32MB', however the parameter available will allow setting the |
| 1685 | * amount to something other than '32MB'. |
| 1686 | * |
| 1687 | * Failure might mean that the memory limit is already higher than |
| 1688 | * either the default or the amount given. It could mean that ini_set |
| 1689 | * and/or ini_get has been disabled and either getting and/or setting |
| 1690 | * could not be accomplished. |
| 1691 | * |
| 1692 | * @since 2.5 |
| 1693 | * @access private |
| 1694 | * |
| 1695 | * @param string $mib MiB amount as a string with MiB amount followed by 'M'. Default is 32M. |
| 1696 | * @return bool True if the amount was set by this function or already 32MB, false on failure. |
| 1697 | */ |
| 1698 | function _set_memory_limit($mib='32M') { |
| 1699 | if( !function_exists('memory_get_usage') ) |
| 1700 | return null; |
| 1701 | |
| 1702 | if( (int) @ini_get('memory_limit') < absint($mib) ) |
| 1703 | @ini_set('memory_limit', $mib); |
| 1704 | |
| 1705 | if( @ini_get('memory_limit') == $mib ) |
| 1706 | return true; |
| 1707 | |
| 1708 | return false; |
| 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * wp_get_memory_limit() - Return the PHP memory limit in MiB |
| 1713 | * |
| 1714 | * If null is returned, then --enable-memory-limit directive was |
| 1715 | * not enabled during PHP install (< 5.2.1). Prior to PHP 5.2.1 |
| 1716 | * if --enable-memory-limit was not used during the install of |
| 1717 | * PHP, then memory_get_usage() would not exist in PHP. |
| 1718 | * |
| 1719 | * So Failure (false return value) might also mean that |
| 1720 | * memory_limit is unavailable on PHP version greater than PHP |
| 1721 | * 5.2.1. |
| 1722 | * |
| 1723 | * @todo Complete edge cases which have the memory limit set to something |
| 1724 | * other than 'M'. The Memory limit can be an integer |
| 1725 | * |
| 1726 | * @since 2.5 |
| 1727 | * |
| 1728 | * @return bool|int False if memory limit is not already in MiB or the MiB amount |
| 1729 | */ |
| 1730 | function wp_get_memory_limit() { |
| 1731 | if( !function_exists('memory_get_usage') ) |
| 1732 | return null; |
| 1733 | |
| 1734 | $iMemoryLimit = @ini_get('memory_limit'); |
| 1735 | |
| 1736 | if( false !== stripos($iMemoryLimit, 'm') ) |
| 1737 | return absint($iMemoryLimit); |
| 1738 | |
| 1739 | return false; |
| 1740 | } |
| 1741 | |