Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP Image upload , Saving link to Database complete tutorial

PHP Image upload , Saving link to Database complete tutorial

This blog is dedicated to all our visitors who asked me to publish a working tutorial on  image upload  using PHP and Saving its path to Mysql Table.

PHP Image upload , Saving link to Database

How to do that :
First we will create a file called install.php , it will create a database called
myimages and a table  named imgtables and also create a folder called gallery where we are going to store  images.

Create a file name install.php
——————————————————

<?php
/*
file name : Install.php
purpose : creating a database name myimages
and table imgtables
Create by : Sanjay Prasad
[email protected]
http://www.openplus.in
*/$db=mysqli_connect(“localhost”,”root”,””);

if($db->connect_errno){
echo “Error <br/>”.$sp->error;
}

$query=”create database if not exists myimages”;

if($db->query($query)){
echo “Created database myimages ..<br/>”;

$sp=mysqli_connect(“localhost”,”root”,””,”myimages”);
if($sp->connect_errno){
echo “Error <br/>”.$sp->error;
}

$tbquery=”create table if not exists imgtables(
id int unsigned auto_increment primary key,
imgurl varchar(255),
date varchar(100)

)engine=’InnoDB'”;

if($sp->query($tbquery)){
echo “Table imgtables created Successfully  ..<br/> “;
}else{
echo “Error <br/>”.$sp->error;
}

//creating directory gallery

if(mkdir(“gallery”,0777)){
echo “Folder gallery create successfully”;
}else{
echo “Error Creating Directory”;
}

}
else{
echo “Error <br/>”.$sp->error;
}

?>

Now We have to create a file name gallery.php, which will upload images to folder gallery and saved their name like flower.jpg on table filed imgurl.

gallery.php

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>File Uploading</title>
<meta charset=”utf-8″ />
</head><body>
<?php
$sp=mysqli_connect(“localhost”,”root”,””,”myimages”);
if($sp->connect_errno){
echo “Error <br/>”.$sp->error;
}$path=”gallery/”;

if(isset($_POST[‘upload’]))
{

$path=$path.$_FILES[‘file_upload’][‘name’];

if(move_uploaded_file($_FILES[‘file_upload’][‘tmp_name’],$path))
{
echo ” “.basename($_FILES[‘file_upload’][‘name’]).” has been uploaded<br/>”;
echo ‘<img src=”gallery/’.$_FILES[‘file_upload’][‘name’].'” width=”48″ height=”48″/>’;
$img=$_FILES[‘file_upload’][‘name’];
$query=”insert into imgtables (imgurl,date) values(‘$img’,now())”;
if($sp->query($query)){
echo “<br/>Inserted to DB also”;
}else{
echo “Error <br/>”.$sp->error;
}

}
else
{
echo “There is an error,please retry or ckeck path”;
}
}
?>
<form action=”gallery.php” method=”post” enctype=”multipart/form-data”>
<table width=”384″ border=”1″ align=”center”>
<tr>
<td width=”108″>Select File</td>
<td width=”260″><label>
<input type=”file” name=”file_upload”>
</label></td>
</tr>
<tr>
<td><label>
<input type=”submit” name=”upload” value=”Upload File”>
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body></html>

 

PHP Do While Tutorial with Working Example

PHP Do While Tutorial with Working Example

On reaching the control to do statement,
the program proceeds to evaluated the body of the loop first. At the
end of the loop, the test-condition in the while statement is
evaluated,so it is categorized as post tested loop.
The basic format of
do while() statement is:
do{
body of the loop
}while(test-conditon);Lets have a working example
a program to enter a number and check entered number is Armstrong or Not (153 is a Armstrong number).

<?php  
if(isset($_POST[‘b1’])) {
                                $number=$_POST[‘t1’];
                                $cp=$number;
                                $digit=0;
                                $cube_plus=0;
                                do{
                                   $digit=$number%10;
                                   $cube_plus=$cube_plus+($digit*$digit*$digit);
                                   $number=(int)($number/10);
                                  }while($number>0);
                                 if($cube_plus==$cp)
                                 $msg=”Armstrong”;
                                 else
                                 $msg=”Not Armstrong”;
                }
                ?>
                <form action=”” method=”post”>
                Enter Number<input type=”text” name=”t1″><br>
                Status<input type=”text” value=”<?php echo $msg;?>”><br>
                <input type=”submit” name=”b1″ value=”Check”>
                </form>


PHP Switch Statement Tutorial with Practical Example

PHP Switch Statement Tutorial with Practical Example

What is PHP Switch Statement?

According to php.net The Switch Statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the  switch statement is for.

PHP Switch  Statement Syntex

switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}

 

PHP Switch  Statement Practical Example

Objective : Creating a program to enter two number and select task from list then display the result.

Coding
<?php
$n1=$_POST[‘t1’];
$n2=$_POST[‘t2’];
$ch=$_POST[‘choice’];
if(isset($_POST[‘done’]))
{
switch($ch)
{
case ‘add’:
$res=$n1+$n2;
break;
case ‘sub’:
$res=$n1-$n2;
break;
case ‘mul’:
$res=$n1*$n2;
break;
case ‘div’:
$res=$n1/$n2;
break;
}
}
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
Enter First Number<input type=”text” name=”t1″><br>
Enter Second Number<input type=”text” name=”t2″><br>
Result<input type=”text” value=”<?php echo $res;?>”><br>
Select Task:&nbsp;&nbsp;&nbsp;
<input type=”radio” name=”choice” value=”add”>ADD
<input type=”radio” name=”choice” value=”sub”>SUB
<input type=”radio” name=”choice” value=”mul”>MUL
<input type=”radio” name=”choice” value=”div”>DIV<br>
<input type=”submit” name=”done” value=”DONE”>
</form>
</body>
</html>

PHP File Uploading easy to learn tutorial with complete script

PHP File Uploading easy to learn tutorial with complete script

PHP is world’s no. 1 server side scripting language, it comes with plenty of features and one of the most famous one is file uploading which is extensively used world wide.

Self Explanatory PHP File Uploading Example

First Create a directory called  gallary/ .
Save this file as gallery.php

<!DOCTYPE html>

<html lang=”en”>
<head>
<title>File Uploading</title>
<meta charset=”utf-8″ />
</head><body>
 <?php
$path=”gallery/”;
$path=$path.$_FILES[‘file_upload’][‘name’];
if(isset($_POST[‘upload’]))
{
if(move_uploaded_file($_FILES[‘file_upload’][‘tmp_name’],$path))
{
echo “The file “.basename($_FILES[‘file_upload’][‘name’]).” has been uploaded”;
}
else
{
echo “There is an error,please retry or ckeck path”;
}
}
?>
<form action=”gallery.php” method=”post” enctype=”multipart/form-data”>
<table width=”384″ border=”1″ align=”center”>
<tr>
<td width=”108″>Select File</td>
<td width=”260″><label>
<input type=”file” name=”file_upload”>
</label></td>
</tr>
<tr>
<td><label>
<input type=”submit” name=”upload” value=”Upload File”>
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body></html>

How To find out Visitors IP ADDRESS using PHP

How To find out Visitors IP ADDRESS using PHP

I f you a PHP programmer then you will find it very easy to track your visitors IP Address, if someone spaming on your site you can also block him. PHP is alway easy and powerful than other scripting language lets check the whole code .Codding:

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>IP Address</title>
<meta charset=”utf-8″ />
</head>
<body>
<?php
$ip=$_SERVER[‘REMOTE_ADDR’];
echo “<br/>Your IP Address is “.$ip;
// suppose IP 127.0.0.1 for localhost and it redirects to prevent spam
if($ip==’127.0.0.1′)
{
echo “You are blocked for Spaming”;
header(“refresh:5; http://www.openplus.in”);
}
?>
</body>
</html> 

 

PHP Constant tutorial with practicle example

PHP Constant tutorial with practicle example

Constant: Constant refers to fixed value that does not change the value
during the execution of program. Constant are defined using PHP’s
define () function which accept two arguments. The $ prefixed is not
required for constant.A simple Example :

<?php
//define constant
define(‘PI’,3.14);
$r=5;
//use constant
$area=PI*$r*$r;
echo “Area of circle=”.$area;
?>

 

Result – 78.5

Practical example:
This script is used for data base connection.

<?php
define(‘MYSQL_HOST’,’localhost’);
define(‘MYSQL_USER’,’root’);
define(‘MYSQL_PASS’,”);
?>

Validate an E-Mail Address with PHP, complete script

Validate an E-Mail Address with PHP, complete script

Validate an E-Mail Address with PHPThis
tutorial will teach you how to validate a e-mail id using regular
expression to control email Spam. In this tutorial I used ternary
operator to filter the values coming from the input box and avoid
errors.Complete Coding is here 

<?php
$email=(isset($_POST['email']))?trim($_POST['email']):'';
function verify($email){
if(!preg_match('/^[_A-z0-9-]+((.|+)[_A-z0-9-]+)*@[A-z0-9-]+(.[A-z0-9-]+)*(.[A-z]{2,4})$/',$email)){
return false;
} else {
return $email;
}
}
if(isset($_POST['submit'])){
if(verify($email)){
echo $email." is a valid E-mail ID";
} else {
echo $email." is an invalid E-mail ID";
}
}
?>
<form action="" method="post"> <input type="text" name="email" value="<?php echo $email;?>"/><br/> <input type="submit" name="submit" value="validate" /> </form>