Friday, 17 May 2013

PHP file upload and Saving

PHP file upload and Saving:

                PHP file upload and saving in server is simple. We need to have a HTML form and PHP script to get the file and upload to server. Here i am going to tell you the step by step guidance to do this.

  HTML form:

<form action="file.php" method="post" enctype="multipart/form-data">
//First start aform tag, Method may be POST or GET. POST method is recommended since it is secured, Then encryption type
<label>File Name:</label>
//this is jus a label
<input type="file" name="myfile" id="myfile"><br>
//file is the tag to upload files
<input type="submit" name="submit" value="Submit">
//this is normal submit button to submit the form
//that's it

Now we have to write PHP script to do further things.

PHP Script(file.php); 

The following code is to just verify whether file is uploaded correctly or not. Later i will show you how to store the uploaded file into server.
if ($_FILES["myfile"]["error"] > 0)
//first check whether errors are there or not. If not then proceed
  echo "Sorry Have some Error: " . $_FILES["myfile"]["error"] . "<br>";
//Displays error message and exact reason for error
//if no error then display details of file
echo "Well your file has been uploaded successfully in to server";
  echo "File name: " . $_FILES["myfile"]["name"] . "<br>";
  echo "Your File Type: " . $_FILES["myfile"]["type"] . "<br>";
  echo "Your File Size: " . ($_FILES["myfile"]["size"] / 1024) . " kB<br>";
  echo "Your file stored : " . $_FILES["myfile"]["tmp_name"];
That is it. Now we move on to PHP script to to store files in server Location

 PHP Script:(to store in file)

  As everyone do, We also Store files in Upload folder. Here is the code for that. Just copy and paste it after above code. If you do like this then you can able able to see uploaded file information and success message if uploaded correctly.
if (file_exists("upload/" . $_FILES["myfile"]["name"]))
//we must check whether file is Already exist or not. We must do it for hassle free operation
      echo $_FILES["file"]["name"] . " already exists. ";
     //if it exist then we have to say exist
    // we can upload even if it is already exist by renaming it. I showed you the codes below for that
      move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["myfile"]["name"]);
//it is PHP function to move files to server
      echo "Stored in: " . "upload/" . $_FILES["myfile"]["name"];
//final message to echo stored location

PHP script:(to rename before Store in folder)

 Always we need to rename the files before saving to server. Because users may upload files with same name. If that happens then the old file with same name gets replaced with new one. So we need to rename all files before uploading to server. For that use the below piece of code:  $randomdigit=rand(0000,9999);
//generate a randome number. You may use letters or alphanumeric also
//get the filen ame in to $filename
//Now combine random number and filename to get ne file name
$path= "resumes/".$newfilename;
$path variable holds the location of folder
copy($_FILES["file"]['tmpname'], $path);
//this php function can also upload files to server like move_uploaded_file

Check one more time whether new file name is exist in upload/ folder. If it exists the generate random number one more time and then copy to server.

That is it.


Friday, 3 May 2013

JavaScript Basics

 JavaScript Array:

  Here i am going to Guide you on JavaScript Arrays. Array in JS is Same as in PHP and other Programming Languages.    Here is the Syntax for creating Array,


var fruitArray = new Array( "apple", "orange", "mango","banana" );
var fruitArray = new Array[ "apple", "orange", "mango","banana" ];

 In this Array,

  fruitsarray[1] is the First element,

 fruitsarray[2] is the Second element,

 fruitsarray[3] is the Third element,

 fruitsarray[4] is the Fourth element.


new Date( ):

        This will display Current Date with Time. But We Programmers won't like to display Dates with time. But we will format it with some functions and display it.       
Here is a program to Display current date As many clients wants to display in Web Pages.

Function to display Current Date:  

function displayTodaysDate()
var x = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var z = new Array("January","February","March","April", "May","June",
"July", "August", "September","October","November","December");

var d = new Date();
var day = d.getDay();
var month = d.getMonth();

// Assemble the string to display
var s = x[day] + ", " + d.getDate() + " " + (z[month]) + " " + (d.getFullYear());


Output:  Friday, 3 May 2013

 Just Copy it in your Page and then Call this function using the below code,

<script type="text/javascript">



Other Syntax:

                Please Use any of the below syntax below to get desired date output in javascript
new Date(milliseconds)
new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ])

PHP function to find dates between two dates

PHP function to find dates between two dates

      Here i am going to write a function for finding dates between two Dates using PHP. It will output all the days between two given dates as a array. The main feature of this is,
      ->It will output all days in array format
    You can use this function to find number of days between given two dates as below.

Displaying all days:

            To display all days, just write the below code


Displaying Number of days:

          To display number of days between these two dates just use the below code

Main Function:              

 Here is the function to do all the above required things. Just copy it in your PHP file and display dates as i specified above.  

function createDateRange($strDateFrom,$strDateTo) {
//two dates formatted as YYYY-MM-DD and creates an
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));     
iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));
 if ($iDateTo>=$iDateFrom)     {        
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry         
while ($iDateFrom<$iDateTo)         {             
$iDateFrom+=86400; // add 24 hours             
 }     }     return $aryRange; 
}  ?>