Thursday, 5 September 2013

Print Diamond Pattern through php

//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//for example Diamond_Size=29
//please set Diamond_Size variable to odd numbers only.]

<?php
$Diamond_Size=19;     

if($Diamond_Size%2==1)
{
printUpperTriangle($Diamond_Size);
//echo "inside";
printLowerTriangle($Diamond_Size);
}
else
{
echo "Please set limit to some odd number";
}

    function printUpperTriangle($limit)
    {
        $sp=$limit/2;
        for($i=1,$space=$sp;$i<=$limit;$i+=2,$space--)
        {
            if($space>=0)
            {
            printSpace($space);
                for($j=1;$j<=$i;$j++)
                {
                echo "*";
                }
            }
        echo "<br/>";
        }
    }
    function printLowerTriangle($limit)
    {
        $sp=$limit/2;
        for($i=$limit-2,$space=1;$i>0;$i-=2,$space++)
        {
            if($space<=$sp)
            {
                echo "&nbsp;&nbsp;";
            printSpace($space);
                for($j=1;$j<=$i;$j++)
                {
                echo "*";
                }
            }
        echo "<br/>";
        }
    }
    function printSpace($space)
    {
        for($i=$space;$i>0;$i--)
        {
        echo "&nbsp;&nbsp;";
        }
    }

?>

Wednesday, 4 September 2013

PHP code fro remove file from directory

$str = 'C:/xampp/htdocs/demos/j'; //path
 function recursiveDelete($str){
       if(is_file($str)){
           return @unlink($str);
       }
       elseif(is_dir($str)){
          $scan = glob(rtrim($str,'/').'/*');
          //print_r($scan);
           foreach($scan as $index=>$path){
         
               recursiveDelete($path);
           }
           return @rmdir($str);
       }
   }

recursiveDelete($str);
mkdir($str);//re-create directory

Tuesday, 3 September 2013

Upload Images PHP Code

form.php
<html>
 <head>
  <script type="text/javascript">
    function validate(){
     var filevalue=document.getElementById("file").value;
     var description=document.getElementById("description").value;
     if(filevalue=="" || filevalue.length<1){
         alert("Select File.");
         document.getElementById("file").focus();
         return false;
       }
     if(description=="" || description.length<1){
         alert("File Description must not be blank.");
         document.getElementById("description").focus();
         return false;
      }
   
     return true;   
    }
  </script>
 </head>
<body >
  <h2 align="center"  >File Upload</h2>
  <form action="file_upload.php" method="post"
  enctype="multipart/form-data" onSubmit="return validate()" >
    <table align="center"  >
      <tr>
        <td><label for="file">File:</label></td>
        <td><input type="file" name="file" id="file" /></td>
      </tr>
      <tr>
        <td><label >File Description:</label></td>
        <td><input type="text" name="description" id="description" /></td>
      </tr>
      <tr>
        <td></td>
        <td><input type="submit" name="submit" value="Submit" /></td>
      </tr>
    <table>
 </form>
</body>
</html>

file_uoload.php

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
  {
      if ($_FILES["file"]["error"] > 0)
        {
          echo "File Error :  " . $_FILES["file"]["error"] . "<br />";
        }else {
          echo "Upload File Name:  " . $_FILES["file"]["name"] . "<br />";
          //echo "File Type:         " . $_FILES["file"]["type"] . "<br />";
         // echo "File Size:         " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";         
          echo "File Description:: ".$_POST['description']."<br />";
         
          if (file_exists("images/".$_FILES["file"]["name"]))
            {
               echo "<b>".$_FILES["file"]["name"] . " already exists. </b>";
            }else
            {
               move_uploaded_file($_FILES["file"]["tmp_name"],"images/". $_FILES["file"]["name"]);
               //echo "Stored in: " . "images/" . $_FILES["file"]["name"]."<br />";
               ?>
                 Uploaded File:<br>
                 <div style="width:600px;background-color:#d4d4d4">

                 <img style="float:left;" src="images/<?php echo $_FILES["file"]["name"]; ?>"  width="80" height="100" alt="Image path Invalid" ><div style="float:left;"> <?php echo $_POST['description']; ?></div>
                 </div>
              <?php
           }
        }
      }else
      {
      echo "Invalid file detail ::<br> file type ::".$_FILES["file"]["type"]." , file size::: ".$_FILES["file"]["size"];
    }
?>



after this create one directory  as "images" and place all pages and images folder in main folder and run form.php