Php Image upload Script

This is a basic script to upload images on the server.
Html Code: index.html

<form action="upload.php" method="post" enctype="multipart/form-data">
 Select image to upload:
 <input type="file" name="fileToUpload" id="fileToUpload">
 <input type="submit" value="Upload Image" name="submit">

PHP Code: upload.php

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
 if($check !== false) {
 echo "File is an image - " . $check["mime"] . ".";
 $uploadOk = 1;
 } else {
 echo "File is not an image.";
 $uploadOk = 0;
// Check if file already exists
if (file_exists($target_file)) {
 echo "Sorry, file already exists.";
 $uploadOk = 0;
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
 echo "Sorry, your file is too large.";
 $uploadOk = 0;
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png"
&& $imageFileType != "jpeg" && $imageFileType != "gif" ) {
 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
 $uploadOk = 0;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
 echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
 echo "The file ". basename( $_FILES["fileToUpload"]["name"]).
 " has been uploaded.";
 } else {
 echo "Sorry, there was an error uploading your file.";

php Modulus operation

What is PHP modulus operator?
we can say that modulus is the division reminder of the operation.
In this below example the remainder (the modulus, or technically, the modulo) of 5 divided by 2 is 1, it calculating by checking how much 2 in the 5 and the balance(reminder) will show.

  $a = 5;
  $b = 2;
  $c = $a % $2;
  echo $c; // Output 1

In this more real-world example, the function find() will be called whenever the variable $count divided by 1,000 is zero:

  // some code up here ...
  // php modulus operator
  if ($count % 1000 == 0)
  // more code down here ...

I do something like this when I generate the sitemap files for this website. I try to keep each sitemap file at no more than 1,000 URLs, so I use code like this to stop writing one file, and then start writing the next file.

PHP Learning tutorial class 1

Hope you know what is PHP and who developed it etc like things. We don’t need such thing to learn PHP but when we start with any thing we want to know at least some general basic things regarding the topic.
For beginning our PHP study we need to setup a Web server on our local computer. We want to install the following applications to our system

  • APACHE SERVER (web Server)
  • PHP ( Server Side Scripting Language)
  • MYSQL (Database )
  • PhpMyAdmin ( Database Managment Application)

It is little difficult to install these software separately, so we can install them with the help of server package programs available in various sites with free of cost.
Please go to one of the below links to get the WAMP program – ( Windows Apache MySql PHP) or XAMPP – X indicates Cross platform all other letters are same as before and last additional P indicates Perl it is also a programming language.
Please Download Wamp or Xammp using the below links according to the configuration of PC
Hope you are downloaded the program and installed it by double clicking it. if you need any help for installing the above please free to contact me.
If you chose WAMP we will get a folder named wamp in the hard disk drive you installed our server program. Otherwise xampp is your folder name. Open it by double clicking it we will get a folder named www in wamp and htdocs in xampp. Friends This is our root folder, We are putting our files here to execute.
Write you First php code ( Save it in the folder php/index.php )

 echo "hello world!";

Call the code from url