Sunday, 10 June 2012

E-Mail validation in PHP is now simple

For long time we were using regular expression for e-mail validation in php. Since everyone is not so good with these reg exp, the introduction  of filter in PHP 5 will be a relief. 
There is an option for sanitation in new PHP version. But I dont know, up to what extend this is good in email validation. Email Sanitation helps you to remove some special characters from the given address. Also there are some more sanitation like URL, Numbers etc.
But I think Validation is a better solution than sanitation. Validation for integer, IP etc are also included in this section.
The details are there in the following link.
sanitation and validation
and
http://www.phpro.org/tutorials/Filtering-Data-with-PHP.html#12
                                        I recommend you to use validation too, even if you use the sanitation. The validation is good enough to check @,.(dot). Also it checks for the special characters in local part and domain part differently. Those special characters allowed in local part is not allowed in domain part.
The usage is so simple and easy to understand, since they are inbuilt functions.
Both Sanitation and validation came under PHP Filter.

An example:
<?php
$email = "some{one@exam{ple.com";


if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  }
else
  {
  echo "E-mail is valid";
  }
?> 

Wednesday, 6 June 2012

Uploading a file using php


<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

  <?php
 ************************echo $_FILES["file"]["type"];************************
  echo $_FILES["file"]["name"];
  echo $_FILES["file"]["size"];
  $_SESSION["file"]=$_FILES["file"]["name"];
// this if block is used to check the file extension. Only files with certain extensions are allowed.
  if ((($_FILES["file"]["type"] == "application/force-download")  //for excel file
  || ($_FILES["file"]["type"] == "image/jpeg")  //for .jpeg files
  || ($_FILES["file"]["type"] == "application/pdf"))  //for pdf files

  && ($_FILES["file"]["size"] < 2000000)) //used to limit size of file
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("folder_name/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"folder_name/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "folder_name/" . $_FILES["file"]["name"];
      }
    }
}
  else
  {
  echo "Invalid file";
  }
  ?>
 folder_name: Folder that you need to keep the uploaded file.(directory)

File type for uploading files in php

 

 ********************** This echo is helpful if you dont know the file type that you need to upload. Echo gives you the type of file and that can be used in the if condition.

for eg: You need to upload a file with extension .pdf. In the above code, only certain extensions are allowed.(pdf is allowed here).       echo $_FILES["file"]["type"];   will give you the extension of file you are trying to upload. Just copy and paste the extension in the if condition for accepting that type of files to get uploaded.


Download file using PHP


<?php
if($_REQUEST['Download'])
{
$file = 'filename.extension';
//If the file is not in the same location as that of this code file, then set the path in $file.

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
}
?>
<html>
<body>
<form name="form1" method="post" action="#">
<input type="submit" name="Download" value="Download">
</form>
</body>
</html>


If you have to give the file name as input, do it using a text box. 

Hiding and showing in single button using jquery



<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>


<script type="text/javascript">
$(document).ready(function(){
  $("#b").click(function(){


  if($("p").css('display') == 'block'){
    $("p").hide(1000);
    $(this).val("show");
   }else{
    $("p").show(1000);
    $(this).val("hide");
   }
  });
  $("#s").click(function(){
    $("p").show(1000);
   $("#s").attr("value", "Hide")
   $("#s").attr("id", "b")
  });
});
</script>
</head>
<body>
<input type="button" name="Hide" id="b" value="Hide">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>