Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, August 21, 2013

PHP: step by step example to do a download manager web page

Some info should be concerned before you begin to do a download manager program:
1. Bandwidth of your host
2. Download speed
3. Capacity of server
4. File is stored in folder or Database? If it is store in DB, should concern about capacity of DB
5. Download statistic

The first program
//file download.php
<?php
 $filename = "document.zip";
 $fp = fopen($filename, "rb");

 header("Content-type: application/octet-stream");
 header("Content-length: " . filesize($filename));

 fpassthru($fp);
 fclose($fp);
 ?>

It's too simple, sure! The first program will read document.zip file in server then save it on client (PC of user) as download.php file as default. You must rename it with correct extension name to open it in client PC.

The second program
Next step, we'll make the program download a file on a specific folder on server then save it in the same name in client, e.g. we want to download and save file with name document.zip on server, we will call download.php?file=document.zip
//file download.php
<?php
 //default folder on server that contains files which are allowed to download
 $upload_dir = "../upload/";

 //get download file name
 $filename = isset($_GET['file'])?$_GET['file']:'';

 //check the file
 if ( !preg_match('/^[a-z0-9\_\-][a-z0-9\_\-\. ]*$/i', $filename) )
         || !is_file($upload_dir.$filename) || !is_readable($upload_dir.$filename) ) {
     echo "Error: invalid file name or not existing!";
     exit(-1);
 } //end if

 //open in binary mode and read the file
 $fp = fopen($upload_dir.$filename, "rb");

 //send header to the browser in client side
 header('Content-type: application/octet-stream');
 header('Content-disposition: attachment; filename="'.$filename.'"');
 header('Content-length: ' . filesize($upload_dir.$filename));

 //get and save file
 fpassthru($fp);
 fclose($fp);
?>

It is still too easy, sure!

Where I can use this download manager
-Use it in the case of you don't want publish the direct location to download the file
-Use it in next step of a login progress, in which you allow user download a file after paying credit
-Use it if you want to track activity of user. This requires small modification to save time stamp + user + client ip + download file name to a log file or DB.

When we shouldn't use it
-Server is overloading
-File is too big
-File is not important

Hope it can help you start a bigger program -:)

Thursday, June 20, 2013

php round to .5 or .25

To round to .5:
$value = round($value * 2, 0) / 2;

To round to .25
$value = round($value * 2, 0) / 4;

Take it easy -:)

Thursday, August 16, 2012

PHP SoapClient calls .NET web service


1. Link to the WSDL output of the .NET web service as below
$client = new SoapClient("http://yourhost/your_web_service.asmx?wsdl");

2. Declare params
$params = array();
$params['param1'] = $value1; //param1 = first variable name in  YourWebMethod  of .Net web service
$params['param2'] = $value2; //param2 = second variable name in  YourWebMethod  of .Net web service
...

3. Call web method / function
$result = $client->YourWebMethod($params)->YourWebMethodResult;

4. Process $result


Tuesday, May 22, 2012

Encrypt by PHP & Decrypt by ASP.NET (C#) and vice versa

Ref: http://stackoverflow.com/questions/1278974/php-encryption-vb-net-decryption

php.ini, enable:
extension=php_mcrypt.dll

Here is my modified code:

PHP file:
<?php

function decryptRJ256($key,$iv,$string_to_decrypt)
{
    $string_to_decrypt = base64_decode($string_to_decrypt);
    $rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);
    $rtn = rtrim($rtn, "\0\4");
    return($rtn);
}

function encryptRJ256($key,$iv,$string_to_encrypt)
{
    $rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);
    $rtn = base64_encode($rtn);
    return($rtn);
}

$ky = 'lkirwf897+22#bbtrm8814z5qq=498j5'; // 32 * 8 = 256 bit key
$iv = '741952hheeyy66#cs!9hjv887mxx7@8y'; // 32 * 8 = 256 bit iv

$text = "Here is my data to encrypt !!!";

$etext = encryptRJ256($ky, $iv, $text);
$dtext = decryptRJ256($ky, $iv, $etext);

header('Location: http://localhost/test.aspx?t='.$etext);
?>

ASP.NET (C#):

public static string DecryptRJ256(string prm_key, string prm_iv, string prm_text_to_decrypt) {

var sEncryptedString = prm_text_to_decrypt;

var myRijndael = new RijndaelManaged() {
 Padding = PaddingMode.Zeros,
 Mode = CipherMode.CBC,
 KeySize = 256,
 BlockSize = 256
};

var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);

var decryptor = myRijndael.CreateDecryptor(key, IV);

var sEncrypted = Convert.FromBase64String(sEncryptedString);

var fromEncrypt = new byte[sEncrypted.Length];

var msDecrypt = new MemoryStream(sEncrypted);
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

return (Encoding.ASCII.GetString(fromEncrypt));
}

public static string EncryptRJ256(string prm_key, string prm_iv, string prm_text_to_encrypt) {

var sToEncrypt = prm_text_to_encrypt;

var myRijndael = new RijndaelManaged() {
 Padding = PaddingMode.Zeros,
 Mode = CipherMode.CBC,
 KeySize = 256,
 BlockSize = 256
};

var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);

var encryptor = myRijndael.CreateEncryptor(key, IV);

var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

var toEncrypt = Encoding.ASCII.GetBytes(sToEncrypt);

csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();

var encrypted = msEncrypt.ToArray();

return (Convert.ToBase64String(encrypted));
}


protected void Page_Load(object sender, EventArgs e)
    {
//Shared 256 bit Key and IV here
string sKy = " lkirwf897+22#bbtrm8814z5qq=498j5 "; //32 chr shared ascii string (32 * 8 = 256 bit)
string sIV = " 741952hheeyy66#cs!9hjv887mxx7@8y "; //32 chr shared ascii string (32 * 8 = 256 bit)

if(Request.QueryString["t"]!=null)
{
string t = Request.QueryString["t"].ToString();
t = t.Trim().Replace(" ", "+");
Button1.Text = DecryptRJ256(sKy, sIV, t);
}
    }



Thursday, March 22, 2012

Concatenating arrays in PHP


Use array_merge to concatenate two numerically-indexed arrays; not array_push and not the array union operator: +.

$first = array('doh', 'ray', 'me');
$second = array('fah', 'soh', 'lah', 'te', 'do');

echo "Union: ", var_export($first + $second, true), "n";
echo "Merge: ", var_export(array_merge($first, $second), true), "n";

// array_push returns int, not an array:
array_push($first, $second);
echo "Push: ", var_export($first, true), "n";
The output:
Union: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 => 'te',
  4 => 'do',
)
Merge: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 => 'fah',
  4 => 'soh',
  5 => 'lah',
  6 => 'te',
  7 => 'do',
)
Push: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 =>
  array (
    0 => 'fah',
    1 => 'soh',
    2 => 'lah',
    3 => 'te',
    4 => 'do',
  ),
)
Subscribe to RSS Feed Follow me on Twitter!