Browsing articles in "PHP"

Reduce time in Uploading and Deleting when managing files through ftp! Making a full directory into .zip and unzipping it on server!

Nov 1, 2009   //   by nuhil   //   CMS, Joomla, PHP, Tips for the New  //  2 Comments

Suppose you are going to upload a big size folder containing a huge number of files on it. Think about joomla. Then if you use ftp client software such as filezilla, it will take about 2 hours if you have internet connection that has avg. speed of 15kbps.

Again if you want to delete such files or directory from that hosting place then it will take 1 hour on that speed configuration.

This is happened because filezilla checks the permission and additional facts of each file by some commands every time it uploads or deletes a file.

We will just give a single file to filzilla to upload and will make several files into a single file when deleting. Thus we can reduce the time that filzilla takes for several files.

Here we go:

First make the desired directory to be uploaded into .zip file on your pc. For example Joomla.zip

The simply upload it to the server by Filezilla. It will take no more than 15 mins in a speed of 15kbps.

Then create a php file:

<?php
  1. // Write an URL on the browser like
  2. // http://www.example.com/project/unzip.php?archive=Joomla.zip
  3.  
  4. $filename = $_GET['archive'];
  5. $zip = new ZipArchive;
  6. if ($zip-&gt;open($filename) === TRUE) {
  7.         $zip-&gt;extractTo('./');
  8.         $zip-&gt;close();
  9.         echo 'The file <strong>'.$filename.'</strong> Extracted successfully';
  10.     }    else {
  11.         echo 'failed';
  12.         }
  13. ?&gt;

Run this script on that same directory and follow the instruction above the code. It will extract that Joomla.zip into Joomla. Then Delete the Joomla.zip in a sec. Thats it.

Now guess, you have uploaded a wrong folder wasting a huge time or you need to replace that directories with your customized files and folders. Now here we talk when deleting such a big directory. We dont know how big and recursive it is.

Create this file:

<?php
  1. function removedir($dirname)
  2.     {
  3.         if (is_dir($dirname))
  4.         $dir_handle = opendir($dirname);
  5.         if (!$dir_handle)
  6.         return false;
  7.         while($file = readdir($dir_handle)) {
  8.             if ($file != "." &amp;&amp; $file != "..") {
  9.                 if (!is_dir($dirname."/".$file))
  10.                 unlink($dirname."/".$file);
  11.                 else
  12.                 {
  13.                     $a=$dirname.'/'.$file;
  14.                     removedir($a);
  15.                 }
  16.             }
  17.         }
  18.         closedir($dir_handle);
  19.         rmdir($dirname);
  20.         return true;
  21.     }
  22.  
  23. $dirname='Joomla';
  24. removedir($dirname);
  25. ?&gt;

This will delete that whole directory whether it is empty or not.

Here is a bonus function that will make a directory into a .zip file. You better know its usage. Just run it on the server and make a .zip copy of a desired directory.

<?php
  1. // increase script timeout value
  2. ini_set('max_execution_time', 300);
  3.  
  4.  // create object
  5. $zip = new ZipArchive();
  6.  
  7. // open archive
  8. if ($zip-&gt;open('file.zip', ZIPARCHIVE::CREATE) !== TRUE)
  9.  {
  10.  die ("Could not open archive");
  11.  
  12.  }
  13.  
  14. // initialize an iterator
  15. // pass it the directory to be processed
  16. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("Joomla/"));
  17.  
  18.  // iterate over the directory
  19. // add each file found to the archive
  20. foreach ($iterator as $key=&gt;$value) {
  21. $zip-&gt;addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
  22.          }
  23. // close and save archive
  24. $zip-&gt;close();
  25. echo "Archive created successfully Named file.zip";
  26. ?&gt;

Rotating Image to any degree with transparent background

Oct 27, 2009   //   by nuhil   //   PHP, Tips for the New  //  1 Comment

The following code will rotate an image to any given degree and if you use png image then the background color can be transparent. So, you can use that rotated image upon any image or color.

<?php
  1.     $filename = 'image.png';
  2.     $degrees = 20;
  3.  
  4.     header('Content-type: image/png');
  5.  
  6.     $source = imagecreatefrompng($filename);
  7.  
  8.     $rotate = imagerotate($source, $degrees, -1); // -1 will cause the bg color as white
  9.  
  10.     imagealphablending($rotate, true);
  11.     imagesavealpha($rotate, true);
  12.  
  13.     imagepng($rotate);
  14. ?>

Sending HTML mail by mail()

Sep 17, 2009   //   by nuhil   //   Featured, HTML, PHP, Tips for the New  //  No Comments
<?php
  1. $to = "nuhilmehdy@gmail.com";
  2. $subject = "HTML Test";
  3. $Name = "Nuhil"; //senders name. // This is just for display name
  4. $email = "email@adress.com"; //senders e-mail adress. // I think this would be generated by the host automatically
  5. $last_name = "Mehdy"; // A test variable for using in the html format
  6.  
  7. $message = '
  8. <html>
  9. <head>
  10. <title>HTML email</title>
  11. </head>
  12. <body>
  13. <p>This email contains HTML Tags!</p>
  14. <table width="100%"  border="1">
  15.  <tr>
  16.    <td>First Name </td>
  17.    <td>Last Name </td>
  18.  </tr>
  19.  <tr>
  20.    <td>Nuhil</td>
  21.    <td>'.$last_name.'</td>
  22.  </tr>
  23. </table>
  24. </body>
  25. </html>
  26. '; // Use ' (single quote) for defining the variable content if you use " (double quote) inside that html format OR use reverse method
  27.  
  28. $headers = "MIME-Version: 1.0" . "\r\n"; // Always set content-type when sending HTML email
  29. $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // This defines the mail as html content
  30.  
  31. // More headers
  32. $headers .= "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
  33. $headers .= "Cc: nuhil@yahoo.com" . "\r\n"; // For Cc sending you can use headers also
  34.  
  35. mail($to,$subject,$message,$headers);
  36. ?>

Converting Numerical Value to its Corresponding Text Format!

Aug 3, 2009   //   by nuhil   //   PHP  //  2 Comments

Some times we need to convert a numerical value to its text value. For example when we want to make a digit or a summation of some value to a text format in the case of any accounting purpose then this code of functions will help.

  1. $nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
  2. "eight", "nine", "ten", "eleven", "twelve", "thirteen",
  3. "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
  4. "nineteen", "twenty", 30 => "thirty", 40 => "forty",
  5. 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
  6. 90 => "ninety" );
  7.  
  8. function int_to_words($x) {
  9. global $nwords;
  10.  
  11. if(!is_numeric($x))
  12. $w = '#';
  13. else if(fmod($x, 1) != 0)
  14. $w = '#';
  15. else {
  16. if($x < 0) {
  17. $w = 'minus ';
  18. $x = -$x;
  19. } else
  20. $w = '';
  21. // … now $x is a non-negative integer.
  22.  
  23. if($x < 21)   // 0 to 20
  24. $w .= $nwords[$x];
  25. else if($x < 100) {   // 21 to 99
  26. $w .= $nwords[10 * floor($x/10)];
  27. $r = fmod($x, 10);
  28. if($r > 0)
  29. $w .= '-'. $nwords[$r];
  30. } else if($x < 1000) {   // 100 to 999
  31. $w .= $nwords[floor($x/100)] .' hundred';
  32. $r = fmod($x, 100);
  33. if($r > 0)
  34. $w .= ' and '. int_to_words($r);
  35. } else if($x < 1000000) {   // 1000 to 999999
  36. $w .= int_to_words(floor($x/1000)) .' thousand';
  37. $r = fmod($x, 1000);
  38. if($r > 0) {
  39. $w .= ' ';
  40. if($r < 100)
  41. $w .= 'and ';
  42. $w .= int_to_words($r);
  43. }
  44. } else {    //  millions
  45. $w .= int_to_words(floor($x/1000000)) .' million';
  46. $r = fmod($x, 1000000);
  47. if($r > 0) {
  48. $w .= ' ';
  49. if($r < 100)
  50. $word .= 'and ';
  51. $w .= int_to_words($r);
  52. }
  53. }
  54. }
  55. return $w;
  56. }
  57.  
  58. //Where should you need to have the text format
  59.  
  60. echo 'There are currently '. int_to_words(100) . ' members logged on.';

The parameter “100″ will be processed by the functions and will output a value : one hundred

Simply Start Bangla Webpage with Database Connectivity!

Jun 25, 2009   //   by nuhil   //   Bangla, PHP, Unicode  //  No Comments

Created by Sagor Sugested by Nuhil

You can make a Web app using Bangla Interface simply by using one kind of Unicode Font write such as
Avro Keyboard. But there is a restriction. In the client PC , the page will not be shown properly if there does not exist any bangla font installed. So, you can give a link in that page to download a font.

You can also get the benifit of MySQL using simple Queries. Define the database and table datatype as utf8_general_ci . Then in database configuration file write these two line after mysql_select_db().

mysql_query(‘SET CHARACTER SET utf8′,$con);
mysql_query(“SET SESSION collation_connection=’utf8_general_ci’”,$con); //$con is DB configuration lines

Now you can do normal PHP-MySQL tasks in your application. Here is an example named Bangla.zip in Share Box

Pages:1234»