Thursday 20 November 2014

Php File Upload

How to Upload file in Php?


<html>
    <title>File Upload</title>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="pic">
            <input type="submit" value="Add" name='add'>
        </form>
        <?php
            $con=mysql_connect("localhost","root","");
            mysql_select_db("Mydatabase",$con);
            if(isset($_POST['add']))
            {

/*File Upload start from here */
                    $input=$_FILES["pic"]["tmp_name"];
                    $directory="uploads/";     // folder name in which file will store
                    $file_name=$_FILES["pic"]["name"]; 
                    move_uploaded_file($input,$directory.$file_name);
                    $file=$directory.$file_name;
/*File Upload Ends here */

                    $qry="insert into Mytable(filename)value('$file')";
                    if(mysql_query($qry))
                    {
                        echo "Successful";
                    }
                    else
                    {
                        echo "Try Again";
                    }
            }
        ?>
    </body>
</html>

PHP Loops & Conditional Statements

PHP Loops


For: We can say for loop is similar to while only difference is that in For loop we already have the information reagarding the no. Of time loop run. But while is applied where we do not have information that how many time loop has to run.

<?php

for($i=0;$i<=10;$i++)

{

echo $i;

echo"<br>";

}

?>

While: PHP "while" loops will run a block of code over and over again as long as a condition is true.


<?php
$i=1;
while($i<=8d)
{
$i++;
echo $i."<br>";
}
?>


Do..While: Do..while is similar like while, only Difference is that in Do..while condition is cheched after the running of each block of code or after the statement.


<?php
$i=1;
do
{
$i++;
echo $i."<br>";
}
while($i<=10);
?>


PHP Conditional Statements


In PHP we have the following conditional statements:


if statement
- executes some code only if a specified condition is true


<?php

$x=10;

if($x==10)

{

echo "Condition is true";

}

?>



if...else statement- executes some code if a condition is true and another code if the condition is false

<?php

$x=10;

if($x=="11")

{

echo "Condition is true";

}

else

{

echo "Condition is False";

}

?>



if...elseif....else statement- specifies a new condition to test if the first condition is false

<?php

$x=30;

if($x=="10")

{

echo "This is if part"; //condition is false

}

elseif($x=="20")

{

echo "This is elseif part"; //condition is false

}

else

{

echo "This is else part"; //condition is true

}

?>



switch statement- selects one of many blocks of code to be executed

<?php
$favcolor = "red";
switch ($favcolor)
{

case "red":
echo "Your favorite color is red!";
break;

case "blue":
echo "Your favorite color is blue!";
break;

case "green":
echo "Your favorite color is green!";
break;

default:
echo "Your favorite color is neither red, blue, or green!";
}
?>

PHP Variables

PHP Variables
A variable starts with the $ sign, followed by the name of the variable like $abc.
A variable name must begin with a letter or the underscore character. Some valid names are $_abc, $a12 etc.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
<?php
$x=3;
$y=4;
$z=$x+$y;
echo $z;
?>

Local Scope
If we declare a variable inside a function, then its scope is local and it can only be accessed with in the function. Local variables are deleted as soon as the function is executed.
<?php
$var=5; // global scope
function test()
{
echo $var; // local scope
}
test();
?>

Global ScopeIf the variable is declared outside of a function then its scope is global and if we want to access a global variable inside a function, we need to useglobalkeyword.
<?php
$a=5; // global scope
$b=1; // global scope
function test()
{
global $a,$b;
$b=$a+$b;
}
echo $b; // output is 1
test();
echo $b; // output is 6
?>

Static ScopeIf you want a local variable not to be deleted after the function is executed, you can declare it as static. The static variable is however local to the function only and can’t be referenced in other functions. We can use static variable to count the number of times a function is executed.
<?php
function test()
{
static $x=0;
echo $x;
$x++;
}
function test1()
{
echo $x; //this won't print anything because $x is static but local to test()
$x++;
}
test();
test1();
test();
?>

What is PHP

What is PHP?
PHP is a server side scripting language and is used widely for making dynamic pages. PHP stands for “PHP Hypertext Processor” and its an open source scripting language. Developed by Rasmus Lerdorf in 1994. PHP scripts are run on server which means it is server-side language and its supported by major web servers like Apache, ISS and Nginx, making it a great tool to create beautiful dynamic web pages.

PHP Syntax

PHP script starts with <?php and ends with ?>and PHP file default extension is “.php”.

<?php
// PHP code
?>
Each statement ends with a semicolon and two basic statements used to Show the output text is echo and print.Comments in PHP
We can write comment in a line with two forward slashes or we can write comments in a block, syntax is shown below.
<html>
<body>
<?php
//Single line comment
/*
Multi line or Block Comment
*/
?>
</body>
</html>