function formatSizeUnits($bytes) {
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Monday, 2 November 2015
PHP- function for format file size unit
PHP alternative syntax for some of its control structures: if, while, for, foreach, and switch
for PHP 4, PHP 5
PHP offers an alternative syntax for some of its control
structures; namely, if,
while, for,
foreach, and switch.
In each case, the basic form of the alternate syntax is to change
the opening brace to a colon (:) and the closing brace to
endif;, endwhile;,
endfor;, endforeach;, or
endswitch;, respectively.
<?php if ($a == 5): ?>A is equal to 5
<?php endif; ?>
In the above example, the HTML block "A is equal to 5" is nested within an
if statement written in the alternative syntax. The
HTML block would be displayed only if $a is equal to 5.
The alternative syntax applies to else and
elseif as well. The following is an
if structure with elseif and
else in the alternative format:
<?php if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else:
echo "a is neither 5 nor 6";
endif;?>
Note:
Mixing syntaxes in the same control block is not supported.
Friday, 23 October 2015
Establish connection with an Amazon S3 client.using PHP
If PHP S3 client connection is not set correctly, you will get following error:
1) Error retrieving credentials from the instance profile metadata server
2) AWS HTTP error: cURL error 60: SSL certificate problem:
For S3 PHP SDK version 3, the correct connection for S3 client as follows:
<?php
require $_SERVER['DOCUMENT_ROOT'] . '/aws/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use Aws\Common\Credentials\Credentials;
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'mykey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecret');
$bucket = 'defashion2u';
try {
$client = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => awsAccessKey,
'secret' => awsSecretKey
),
'http' => [
'verify' => $_SERVER['DOCUMENT_ROOT'] . '/aws/cacert.pem'
]
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
The parameter version, region, credentials and SSL cert must be set.
More info on the ca cert, here
1) Error retrieving credentials from the instance profile metadata server
2) AWS HTTP error: cURL error 60: SSL certificate problem:
For S3 PHP SDK version 3, the correct connection for S3 client as follows:
<?php
require $_SERVER['DOCUMENT_ROOT'] . '/aws/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use Aws\Common\Credentials\Credentials;
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'mykey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecret');
$bucket = 'defashion2u';
try {
$client = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => awsAccessKey,
'secret' => awsSecretKey
),
'http' => [
'verify' => $_SERVER['DOCUMENT_ROOT'] . '/aws/cacert.pem'
]
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
The parameter version, region, credentials and SSL cert must be set.
More info on the ca cert, here
Friday, 9 October 2015
PHP - Inserts HTML line breaks before all newlines in a string
(PHP 4, PHP 5)
nl2br — Inserts HTML line breaks before all newlines in a string
Description string nl2br ( string $string [, bool $is_xhtml = true ] )
Returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).
Parameters
string
The input string.
is_xhtml
Whether to use XHTML compatible line breaks or not.
Return Values
Returns the altered string.
Examples
Example #1 Using nl2br()
nl2br — Inserts HTML line breaks before all newlines in a string
Description string nl2br ( string $string [, bool $is_xhtml = true ] )
Returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).
Parameters
string
The input string.
is_xhtml
Whether to use XHTML compatible line breaks or not.
Return Values
Returns the altered string.
Examples
Example #1 Using nl2br()
<?phpecho nl2br("foo isn't\n bar");?>
The above example will output:
foo isn't<br /> bar
Example #2 Generating valid HTML markup using the
The above example will output:is_xhtml parameter
<?phpecho nl2br("Welcome\r\nThis is my HTML document", false);?>
Welcome<br> This is my HTML document
Subscribe to:
Posts (Atom)